From 47158e028210c63c9d1ea88f8be580f48fa1ccc4 Mon Sep 17 00:00:00 2001 From: chenos Date: Tue, 17 Jan 2023 09:44:53 +0800 Subject: [PATCH] fix: through collection records should not be reset (#1377) * fix: through collection records should not be reset * fix: test error --- .../update-associations-through.test.ts | 66 +++++++++++++++++++ .../core/database/src/update-associations.ts | 13 +++- 2 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 packages/core/database/src/__tests__/update-associations-through.test.ts diff --git a/packages/core/database/src/__tests__/update-associations-through.test.ts b/packages/core/database/src/__tests__/update-associations-through.test.ts new file mode 100644 index 000000000..097ff1c0a --- /dev/null +++ b/packages/core/database/src/__tests__/update-associations-through.test.ts @@ -0,0 +1,66 @@ +import { Database } from '../database'; +import { mockDatabase } from './'; + +describe('update through', () => { + let db: Database; + beforeEach(async () => { + db = mockDatabase(); + await db.clean({ drop: true }); + }); + + afterEach(async () => { + await db.close(); + }); + + it('should not be reset', async () => { + db.collection({ + name: 'c', + autoGenId: true, + fields: [ + { + name: 'id', + type: 'integer', + primaryKey: true, + autoIncrement: true, + } + ], + }); + db.collection({ + name: 'a', + fields: [ + { + type: 'string', + name: 'name', + }, + { + type: 'belongsToMany', + name: 'b', + target: 'b', + through: 'c', + }, + ], + }); + db.collection({ + name: 'b', + fields: [], + }); + await db.sync(); + const b = await db.getRepository('b').create({ + values: {}, + }); + const a = await db.getRepository('a').create({ + values: { + b: [b.toJSON()], + }, + }); + const c1 = await db.getRepository('c').findOne(); + await db.getRepository('a').update({ + filterByTk: a.id, + values: { + b: [b.toJSON()], + }, + }); + const c2 = await db.getRepository('c').findOne(); + expect(c1.get('id')).toBe(c2.get('id')) + }); +}); diff --git a/packages/core/database/src/update-associations.ts b/packages/core/database/src/update-associations.ts index 417ec99a2..7d5e728d3 100644 --- a/packages/core/database/src/update-associations.ts +++ b/packages/core/database/src/update-associations.ts @@ -6,7 +6,7 @@ import { HasOne, Hookable, ModelStatic, - Transactionable, + Transactionable } from 'sequelize'; import { Model } from './model'; import { UpdateGuard } from './update-guard'; @@ -402,6 +402,7 @@ export async function updateMultipleAssociation( const list1 = []; // to be setted const list2 = []; // to be added + const created = []; for (const item of value) { if (isUndefinedOrNull(item)) { continue; @@ -413,6 +414,11 @@ export async function updateMultipleAssociation( } else if (item.sequelize) { list1.push(item); } else if (typeof item === 'object') { + const targetKey = (association as any).targetKey || 'id'; + if (item[targetKey]) { + created.push(item[targetKey]); + list1.push(item[targetKey]); + } list2.push(item); } } @@ -456,8 +462,9 @@ export async function updateMultipleAssociation( continue; } const addAccessor = association.accessors.add; - - await model[addAccessor](item[pk], accessorOptions); + if (!created.includes(item[pk])) { + await model[addAccessor](item[pk], accessorOptions); + } if (!recursive) { continue; }