diff --git a/packages/core/database/src/__tests__/update-associations.test.ts b/packages/core/database/src/__tests__/update-associations.test.ts index dcba399af..a212d5d47 100644 --- a/packages/core/database/src/__tests__/update-associations.test.ts +++ b/packages/core/database/src/__tests__/update-associations.test.ts @@ -7,13 +7,121 @@ describe('update associations', () => { describe('belongsTo', () => { let db: Database; beforeEach(async () => { - db = mockDatabase(); + db = mockDatabase({}); + await db.clean({ + drop: true, + }); }); afterEach(async () => { await db.close(); }); + test('update belongs to with foreign key and object', async () => { + const throughAB = db.collection({ + name: 'throughAB', + fields: [ + { + type: 'belongsTo', + name: 'b', + foreignKey: 'bId', + target: 'B', + }, + ], + }); + + const throughBC = db.collection({ + name: 'throughBC', + fields: [ + { + type: 'belongsTo', + name: 'c', + foreignKey: 'cId', + target: 'C', + }, + ], + }); + + const throughCD = db.collection({ + name: 'throughCD', + fields: [ + { + type: 'belongsTo', + name: 'd', + foreignKey: 'dId', + target: 'D', + }, + ], + }); + + const A = db.collection({ + name: 'A', + fields: [ + { type: 'string', name: 'name' }, + { type: 'hasMany', name: 'throughAB', foreignKey: 'aId', target: 'throughAB' }, + ], + }); + + const B = db.collection({ + name: 'B', + fields: [ + { type: 'string', name: 'name' }, + { type: 'hasMany', name: 'throughBC', foreignKey: 'bId', target: 'throughBC' }, + ], + }); + + const C = db.collection({ + name: 'C', + fields: [ + { type: 'string', name: 'name' }, + { + type: 'hasMany', + name: 'throughCD', + foreignKey: 'cId', + target: 'throughCD', + }, + ], + }); + + const D = db.collection({ + name: 'D', + fields: [{ type: 'string', name: 'name' }], + }); + + await db.sync(); + + const a1 = await A.repository.create({ + values: { + name: 'a1', + throughAB: [ + { + b: { + name: 'b1', + throughBC: [ + { + c: { + name: 'c1', + throughCD: [ + { + d: { + name: 'd1', + }, + }, + ], + }, + }, + ], + }, + }, + ], + }, + }); + + expect( + a1.get('throughAB')[0].get('b').get('throughBC')[0].get('c').get('throughCD')[0].get('d').get('name'), + ).toBe('d1'); + }); + it('post.user', async () => { const User = db.collection<{ id: string; name: string }, { name: string }>({ name: 'users', diff --git a/packages/core/database/src/update-associations.ts b/packages/core/database/src/update-associations.ts index 49b33c39e..0c9d19ca1 100644 --- a/packages/core/database/src/update-associations.ts +++ b/packages/core/database/src/update-associations.ts @@ -200,6 +200,7 @@ function isReverseAssociationPair(a: any, b: any) { return ( sourceAssoc.source.name === targetAssoc.target.name && + sourceAssoc.target.name === targetAssoc.source.name && sourceAssoc.foreignKey === targetAssoc.foreignKey && sourceAssoc.sourceKey === targetAssoc.targetKey ); @@ -339,12 +340,14 @@ export async function updateSingleAssociation( } const instance = await model[createAccessor](value, { context, transaction }); + await updateAssociations(instance, value, { ...options, transaction, associationContext: association, updateAssociationValues: keys, }); + model.setDataValue(key, instance); // @ts-ignore if (association.targetKey) {