mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 21:49:25 +08:00
fix: update association with foreign key (#5756)
This commit is contained in:
parent
bff82856e7
commit
9ddb4c179e
@ -66,6 +66,48 @@ describe('update associations', () => {
|
|||||||
expect(profile1['userId']).toBe(user.id);
|
expect(profile1['userId']).toBe(user.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should update has many association with foreign key', async () => {
|
||||||
|
const User = db.collection({
|
||||||
|
name: 'users',
|
||||||
|
fields: [
|
||||||
|
{ type: 'string', name: 'name' },
|
||||||
|
{ type: 'hasMany', name: 'profiles', 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: {
|
||||||
|
profiles: [
|
||||||
|
{
|
||||||
|
...profileData,
|
||||||
|
userId: null,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
updateAssociationValues: ['profiles'],
|
||||||
|
});
|
||||||
|
|
||||||
|
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 () => {
|
test('update belongs to with foreign key and object', async () => {
|
||||||
const throughAB = db.collection({
|
const throughAB = db.collection({
|
||||||
name: 'throughAB',
|
name: 'throughAB',
|
||||||
|
@ -351,7 +351,11 @@ export async function updateSingleAssociation(
|
|||||||
|
|
||||||
if (updateAssociationValues.includes(key)) {
|
if (updateAssociationValues.includes(key)) {
|
||||||
const updateValues = { ...value };
|
const updateValues = { ...value };
|
||||||
|
|
||||||
|
if (association.associationType === 'HasOne') {
|
||||||
delete updateValues[association.foreignKey];
|
delete updateValues[association.foreignKey];
|
||||||
|
}
|
||||||
|
|
||||||
await instance.update(updateValues, { ...options, transaction });
|
await instance.update(updateValues, { ...options, transaction });
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -536,6 +540,10 @@ export async function updateMultipleAssociation(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (updateAssociationValues.includes(key)) {
|
if (updateAssociationValues.includes(key)) {
|
||||||
|
if (association.associationType === 'HasMany') {
|
||||||
|
delete item[association.foreignKey];
|
||||||
|
}
|
||||||
|
|
||||||
await instance.update(item, { ...options, transaction });
|
await instance.update(item, { ...options, transaction });
|
||||||
}
|
}
|
||||||
await updateAssociations(instance, item, {
|
await updateAssociations(instance, item, {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user