Skip to content

Commit

Permalink
fix(core): support unsetting composite FKs via flush
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Mar 16, 2024
1 parent 2fba5ee commit 64f2afd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/core/src/drivers/DatabaseDriver.ts
Expand Up @@ -315,6 +315,13 @@ export abstract class DatabaseDriver<C extends Connection> implements IDatabaseD
return;
}

if (prop.joinColumns?.length > 1 && data[k] == null) {
delete data[k];
prop.joinColumns.forEach((joinColumn, idx) => data[joinColumn] = null);

return;
}

if (prop.customType && convertCustomTypes && !this.platform.isRaw(data[k])) {
data[k] = prop.customType.convertToDatabaseValue(data[k], this.platform, { fromQuery: true, key: k, mode: 'query-data' });
}
Expand Down
32 changes: 32 additions & 0 deletions tests/issues/GHx13.test.ts
Expand Up @@ -108,3 +108,35 @@ test('Query through nested relationship', async () => {

expect(submissionFields).toHaveLength(1);
});

test('Setting relationship to null should clear both fields of composite foreign key', async () => {
const submission = await orm.em.findOneOrFail(
FormSubmission,
{ orgId: 1, id: 20 },
{ populate: ['form'] },
);

expect(submission.form).not.toBeNull();

submission.form = undefined;

await orm.em.flush();
orm.em.clear();

const submissionAfter = await orm.em.findOneOrFail(
FormSubmission,
{ orgId: 1, id: 20 },
{ populate: ['form'] },
);

expect(submissionAfter.form).toBeNull();

const qb = orm.em.createQueryBuilder(FormSubmission)
.where({ orgId: 1, id: 20 });

const results = await qb.execute('all', { mapResults: false });
const result = results[0];

expect(result.form_org_id).toBeNull();
expect(result.form_id).toBeNull();
});

0 comments on commit 64f2afd

Please sign in to comment.