fix(database): update association values with nested associations (#1970)
* fix: update association values with nested associations * fix: isReverseAssociationPair
This commit is contained in:
parent
1aad355950
commit
4b9150d448
@ -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',
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user