Merge branch 'main' into next

This commit is contained in:
nocobase[bot] 2024-11-29 14:18:51 +00:00
commit 0e019d1317
2 changed files with 43 additions and 1 deletions

View File

@ -26,6 +26,46 @@ describe('update associations', () => {
await db.close();
});
it('should update has one association with foreign key', async () => {
const User = db.collection({
name: 'users',
fields: [
{ type: 'string', name: 'name' },
{ type: 'hasOne', name: 'profile', foreignKey: 'userId', target: 'profiles' },
],
});
const Profile = db.collection({
name: 'profiles',
fields: [
{ type: 'string', name: 'name' },
{ type: 'bigInt', name: 'userId' },
{ type: 'belongsTo', name: 'user', foreignKey: 'userId' },
],
});
await db.sync();
// create user
const user = await User.repository.create({ values: { name: 'user1' } });
const profile = await Profile.repository.create({ values: { name: 'profile1' } });
const profileData = profile.toJSON();
await User.repository.update({
filterByTk: user.id,
values: {
profile: {
...profileData,
userId: null,
},
},
updateAssociationValues: ['profile'],
});
const profile1 = await Profile.repository.findOne({ filterByTk: profile.id });
expect(profile1['userId']).toBe(user.id);
});
test('update belongs to with foreign key and object', async () => {
const throughAB = db.collection({
name: 'throughAB',

View File

@ -351,7 +351,9 @@ export async function updateSingleAssociation(
}
if (updateAssociationValues.includes(key)) {
await instance.update(value, { ...options, transaction });
const updateValues = { ...value };
delete updateValues[association.foreignKey];
await instance.update(updateValues, { ...options, transaction });
}
await updateAssociations(instance, value, {