Short fix for the morph case. 

Was missing the logic for the joinColumnName: we need to add the
"ObjcectMetadataName" on top the the "FieldMetadataName" for morph
relation like it was designed on fieldmetadata service.

On the typescirpt side: the "as FieldMetadataRelationSettings" is not
ideal but I inherit from the a as FieldMetadataInterface type that does
not know the as FieldMetadataType from its parent. If you have a better
simple idea, let me know.


Note : I think we could refactor a bit this part later on.
This commit is contained in:
Guillim
2025-07-21 18:29:23 +02:00
committed by GitHub
parent e22eef0d68
commit 37d7996a04

View File

@ -4,6 +4,7 @@ import { FieldMetadataType } from 'twenty-shared/types';
import { FindOptionsRelations, ObjectLiteral } from 'typeorm'; import { FindOptionsRelations, ObjectLiteral } from 'typeorm';
import { ObjectRecord } from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface'; import { ObjectRecord } from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
import { FieldMetadataRelationSettings } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata-settings.interface';
import { RelationType } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-type.interface'; import { RelationType } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-type.interface';
import { import {
@ -135,7 +136,7 @@ export class ProcessNestedRelationsV2Helper {
} }
const relationType = sourceFieldMetadata.settings?.relationType; const relationType = sourceFieldMetadata.settings?.relationType;
const { targetRelationName, targetObjectMetadata } = const { targetRelationName, targetObjectMetadata, targetRelation } =
this.getTargetObjectMetadata({ this.getTargetObjectMetadata({
objectMetadataMaps, objectMetadataMaps,
parentObjectMetadataItem, parentObjectMetadataItem,
@ -170,12 +171,21 @@ export class ProcessNestedRelationsV2Helper {
: `${sourceFieldName}Id`, : `${sourceFieldName}Id`,
}); });
const fieldMetadataTargetRelationColumnName =
targetRelation &&
isFieldMetadataEntityOfType(
targetRelation,
FieldMetadataType.MORPH_RELATION,
)
? `${(targetRelation?.settings as FieldMetadataRelationSettings)?.joinColumnName}`
: `${targetRelationName}Id`;
const { relationResults, relationAggregatedFieldsResult } = const { relationResults, relationAggregatedFieldsResult } =
await this.findRelations({ await this.findRelations({
referenceQueryBuilder: targetObjectQueryBuilder, referenceQueryBuilder: targetObjectQueryBuilder,
column: column:
relationType === RelationType.ONE_TO_MANY relationType === RelationType.ONE_TO_MANY
? `"${targetRelationName}Id"` ? `"${fieldMetadataTargetRelationColumnName}"`
: 'id', : 'id',
ids: relationIds, ids: relationIds,
limit: limit * parentObjectRecords.length, limit: limit * parentObjectRecords.length,
@ -191,7 +201,7 @@ export class ProcessNestedRelationsV2Helper {
sourceFieldName, sourceFieldName,
joinField: joinField:
relationType === RelationType.ONE_TO_MANY relationType === RelationType.ONE_TO_MANY
? `${targetRelationName}Id` ? `${fieldMetadataTargetRelationColumnName}`
: 'id', : 'id',
relationType, relationType,
}); });
@ -259,12 +269,17 @@ export class ProcessNestedRelationsV2Helper {
); );
} }
const targetRelation =
objectMetadataMaps.byId[
targetFieldMetadata.relationTargetObjectMetadataId
]?.fieldsById[targetFieldMetadata.relationTargetFieldMetadataId];
const targetRelationName = const targetRelationName =
objectMetadataMaps.byId[ objectMetadataMaps.byId[
targetFieldMetadata.relationTargetObjectMetadataId targetFieldMetadata.relationTargetObjectMetadataId
]?.fieldsById[targetFieldMetadata.relationTargetFieldMetadataId]?.name; ]?.fieldsById[targetFieldMetadata.relationTargetFieldMetadataId]?.name;
return { targetRelationName, targetObjectMetadata }; return { targetRelationName, targetObjectMetadata, targetRelation };
} }
private getUniqueIds({ private getUniqueIds({