fix: set belongs to many associations (#5759)

This commit is contained in:
ChengLei Shao 2024-11-30 20:40:22 +08:00 committed by GitHub
parent 0e2b6343c2
commit 9166b7acf3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 3 deletions

View File

@ -133,6 +133,49 @@ describe('date-field', () => {
await db.close();
});
it('should set default value if collection is middle table in belongs to many association', async () => {
const middleTable = db.collection({
name: 'test_middle',
fields: [{ name: 'date1', type: 'datetimeTz', defaultToCurrentTime: true, allowNull: false }],
});
const sourceTable = db.collection({
name: 'test_source',
fields: [
{
name: 'name',
type: 'string',
},
{
name: 'target',
target: 'test_target',
type: 'belongsToMany',
through: 'test_middle',
},
],
});
const targetTable = db.collection({
name: 'test_target',
fields: [
{ name: 'name', type: 'string' },
{ name: 'source', target: 'test_source', type: 'belongsToMany', through: 'test_middle' },
],
});
await db.sync();
const sourceInstance = await sourceTable.repository.create({ values: { name: 'source' } });
const targetInstance = await targetTable.repository.create({ values: { name: 'target' } });
await sourceTable.repository.update({
values: {
target: [targetInstance.get('id')],
},
filter: { id: sourceInstance.get('id') },
});
});
it('should set default to current time', async () => {
const c1 = db.collection({
name: 'test11',

View File

@ -421,7 +421,7 @@ export async function updateMultipleAssociation(
const createAccessor = association.accessors.create;
if (isUndefinedOrNull(value)) {
await model[setAccessor](null, { transaction, context, individualHooks: true });
await model[setAccessor](null, { transaction, context, individualHooks: true, validate: false });
model.setDataValue(key, null);
return;
}
@ -433,7 +433,7 @@ export async function updateMultipleAssociation(
}
if (isStringOrNumber(value)) {
await model[setAccessor](value, { transaction, context, individualHooks: true });
await model[setAccessor](value, { transaction, context, individualHooks: true, validate: false });
return;
}
@ -472,7 +472,7 @@ export async function updateMultipleAssociation(
}
// associate targets in lists1
await model[setAccessor](setItems, { transaction, context, individualHooks: true });
await model[setAccessor](setItems, { transaction, context, individualHooks: true, validate: false });
const newItems = [];