diff --git a/packages/repository/src/__tests__/integration/repositories/resolve-has-many-metadata.integration.ts b/packages/repository/src/__tests__/integration/repositories/resolve-has-many-metadata.integration.ts index 40f244388377..bcf494ca11ac 100644 --- a/packages/repository/src/__tests__/integration/repositories/resolve-has-many-metadata.integration.ts +++ b/packages/repository/src/__tests__/integration/repositories/resolve-has-many-metadata.integration.ts @@ -29,18 +29,17 @@ describe('keyTo and keyFrom with resolveHasManyMetadata', () => { }); }); - it('resolves metadata using keyTo, but not keyFrom', () => { + it('infers keyFrom if it is not provided', () => { const meta = resolveHasManyMetadata(Category.definition.relations[ 'items' ] as HasManyDefinition); - expect(meta).to.not.have.property('keyFrom'); - expect(meta).to.eql({ name: 'items', type: 'hasMany', targetsMany: true, source: Category, + keyFrom: 'id', target: () => Item, keyTo: 'categoryId', }); diff --git a/packages/repository/src/__tests__/integration/repositories/resolve-has-one-metadata.integration.ts b/packages/repository/src/__tests__/integration/repositories/resolve-has-one-metadata.integration.ts index 60d4b974fc09..3db13b0d66b4 100644 --- a/packages/repository/src/__tests__/integration/repositories/resolve-has-one-metadata.integration.ts +++ b/packages/repository/src/__tests__/integration/repositories/resolve-has-one-metadata.integration.ts @@ -29,18 +29,17 @@ describe('keyTo and keyFrom with resolveHasOneMetadata', () => { }); }); - it('resolves metadata using keyTo, but not keyFrom', () => { + it('infers keyFrom if it is not provided', () => { const meta = resolveHasOneMetadata(Category.definition.relations[ 'item' ] as HasOneDefinition); - expect(meta).to.not.have.property('keyFrom'); - expect(meta).to.eql({ name: 'item', type: 'hasOne', targetsMany: false, source: Category, + keyFrom: 'id', target: () => Item, keyTo: 'categoryId', }); diff --git a/packages/repository/src/relations/has-many/has-many.helpers.ts b/packages/repository/src/relations/has-many/has-many.helpers.ts index 5e27fb321546..702f44721e43 100644 --- a/packages/repository/src/relations/has-many/has-many.helpers.ts +++ b/packages/repository/src/relations/has-many/has-many.helpers.ts @@ -39,13 +39,6 @@ export function resolveHasManyMetadata( const targetModelProperties = targetModel.definition && targetModel.definition.properties; - // Make sure that if it already keys to the foreign key property, - // the key exists in the target model - if (relationMeta.keyTo && targetModelProperties[relationMeta.keyTo]) { - // The explicit cast is needed because of a limitation of type inference - return relationMeta as HasManyResolvedDefinition; - } - const sourceModel = relationMeta.source; if (!sourceModel || !sourceModel.modelName) { const reason = 'source model must be defined'; @@ -54,8 +47,10 @@ export function resolveHasManyMetadata( const keyFrom = sourceModel.getIdProperties()[0]; - if (relationMeta.keyTo) { - // The explict cast is needed because of a limitation of type inference + // Make sure that if it already keys to the foreign key property, + // the key exists in the target model + if (relationMeta.keyTo && targetModelProperties[relationMeta.keyTo]) { + // The explicit cast is needed because of a limitation of type inference return Object.assign(relationMeta, {keyFrom}) as HasManyResolvedDefinition; } diff --git a/packages/repository/src/relations/has-one/has-one.helpers.ts b/packages/repository/src/relations/has-one/has-one.helpers.ts index c880fd4efbf8..b91f1643d67a 100644 --- a/packages/repository/src/relations/has-one/has-one.helpers.ts +++ b/packages/repository/src/relations/has-one/has-one.helpers.ts @@ -39,13 +39,6 @@ export function resolveHasOneMetadata( const targetModelProperties = targetModel.definition && targetModel.definition.properties; - // Make sure that if it already keys to the foreign key property, - // the key exists in the target model - if (relationMeta.keyTo && targetModelProperties[relationMeta.keyTo]) { - // The explicit cast is needed because of a limitation of type inference - return relationMeta as HasOneResolvedDefinition; - } - const sourceModel = relationMeta.source; if (!sourceModel || !sourceModel.modelName) { const reason = 'source model must be defined'; @@ -54,8 +47,10 @@ export function resolveHasOneMetadata( const keyFrom = sourceModel.getIdProperties()[0]; - if (relationMeta.keyTo) { - // The explict cast is needed because of a limitation of type inference + // Make sure that if it already keys to the foreign key property, + // the key exists in the target model + if (relationMeta.keyTo && targetModelProperties[relationMeta.keyTo]) { + // The explicit cast is needed because of a limitation of type inference return Object.assign(relationMeta, {keyFrom}) as HasOneResolvedDefinition; }