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 { 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 {
@ -135,7 +136,7 @@ export class ProcessNestedRelationsV2Helper {
}
const relationType = sourceFieldMetadata.settings?.relationType;
const { targetRelationName, targetObjectMetadata } =
const { targetRelationName, targetObjectMetadata, targetRelation } =
this.getTargetObjectMetadata({
objectMetadataMaps,
parentObjectMetadataItem,
@ -170,12 +171,21 @@ export class ProcessNestedRelationsV2Helper {
: `${sourceFieldName}Id`,
});
const fieldMetadataTargetRelationColumnName =
targetRelation &&
isFieldMetadataEntityOfType(
targetRelation,
FieldMetadataType.MORPH_RELATION,
)
? `${(targetRelation?.settings as FieldMetadataRelationSettings)?.joinColumnName}`
: `${targetRelationName}Id`;
const { relationResults, relationAggregatedFieldsResult } =
await this.findRelations({
referenceQueryBuilder: targetObjectQueryBuilder,
column:
relationType === RelationType.ONE_TO_MANY
? `"${targetRelationName}Id"`
? `"${fieldMetadataTargetRelationColumnName}"`
: 'id',
ids: relationIds,
limit: limit * parentObjectRecords.length,
@ -191,7 +201,7 @@ export class ProcessNestedRelationsV2Helper {
sourceFieldName,
joinField:
relationType === RelationType.ONE_TO_MANY
? `${targetRelationName}Id`
? `${fieldMetadataTargetRelationColumnName}`
: 'id',
relationType,
});
@ -259,12 +269,17 @@ export class ProcessNestedRelationsV2Helper {
);
}
const targetRelation =
objectMetadataMaps.byId[
targetFieldMetadata.relationTargetObjectMetadataId
]?.fieldsById[targetFieldMetadata.relationTargetFieldMetadataId];
const targetRelationName =
objectMetadataMaps.byId[
targetFieldMetadata.relationTargetObjectMetadataId
]?.fieldsById[targetFieldMetadata.relationTargetFieldMetadataId]?.name;
return { targetRelationName, targetObjectMetadata };
return { targetRelationName, targetObjectMetadata, targetRelation };
}
private getUniqueIds({