-
-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): support overlapping composite FKs with different nullability
Closes #4478
- Loading branch information
Showing
4 changed files
with
144 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Entity, PrimaryKey, ManyToOne, SimpleLogger, PrimaryKeyProp, wrap } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/sqlite'; | ||
import { mockLogger } from '../../helpers'; | ||
|
||
@Entity() | ||
class School { | ||
|
||
@PrimaryKey() | ||
schoolCode!: string; | ||
|
||
} | ||
|
||
@Entity() | ||
class Class { | ||
|
||
[PrimaryKeyProp]?: ['school', 'academicYear', 'classCode']; | ||
|
||
@ManyToOne(() => School, { name: 'school_code', primary: true }) | ||
school!: School; | ||
|
||
@PrimaryKey() | ||
academicYear!: string; | ||
|
||
@PrimaryKey() | ||
classCode!: string; | ||
|
||
} | ||
|
||
@Entity() | ||
class StudentAllocation { | ||
|
||
[PrimaryKeyProp]?: ['studentId', 'academicYear']; | ||
|
||
@PrimaryKey() | ||
studentId!: string; | ||
|
||
@PrimaryKey() | ||
academicYear!: string; | ||
|
||
@ManyToOne(() => School, { name: 'school_code' }) | ||
school!: School; | ||
|
||
@ManyToOne(() => Class, { fieldNames: ['school_code', 'academic_year', 'class_code'], nullable: true }) | ||
class?: Class; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [StudentAllocation], | ||
dbName: `:memory:`, | ||
loggerFactory: options => new SimpleLogger(options), | ||
}); | ||
await orm.schema.createSchema(); | ||
}); | ||
|
||
afterAll(() => orm.close(true)); | ||
|
||
test(`GH issue 4478`, async () => { | ||
expect(await orm.schema.getCreateSchemaSQL()).toMatchSnapshot(); | ||
|
||
const school = orm.em.create(School, { schoolCode: 'abc' }); | ||
orm.em.create(StudentAllocation, { studentId: '1', academicYear: '2023', school }); | ||
orm.em.create(StudentAllocation, { | ||
studentId: '2', | ||
academicYear: '2023', | ||
school, | ||
class: { school, classCode: 'cls', academicYear: '2023' }, | ||
}); | ||
|
||
const mock = mockLogger(orm); | ||
await orm.em.flush(); | ||
expect(mock.mock.calls).toMatchSnapshot(); | ||
orm.em.clear(); | ||
|
||
const sa = await orm.em.find(StudentAllocation, { academicYear: '2023' }, { populate: true }); | ||
expect(wrap(sa[0]).toObject()).toEqual({ | ||
academicYear: '2023', | ||
school: { | ||
schoolCode: 'abc', | ||
}, | ||
studentId: '1', | ||
}); | ||
|
||
expect(wrap(sa[1]).toObject()).toEqual({ | ||
academicYear: '2023', | ||
class: { | ||
academicYear: '2023', | ||
classCode: 'cls', | ||
school: { | ||
schoolCode: 'abc', | ||
}, | ||
}, | ||
school: { | ||
schoolCode: 'abc', | ||
}, | ||
studentId: '2', | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
tests/features/composite-keys/__snapshots__/GH4478.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`GH issue 4478 1`] = ` | ||
"pragma foreign_keys = off; | ||
create table \`school\` (\`school_code\` text not null, primary key (\`school_code\`)); | ||
create table \`class\` (\`school_code\` text not null, \`academic_year\` text not null, \`class_code\` text not null, constraint \`class_school_code_foreign\` foreign key(\`school_code\`) references \`school\`(\`school_code\`) on update cascade, primary key (\`school_code\`, \`academic_year\`, \`class_code\`)); | ||
create index \`class_school_code_index\` on \`class\` (\`school_code\`); | ||
create table \`student_allocation\` (\`student_id\` text not null, \`academic_year\` text not null, \`school_code\` text not null, \`class_code\` text null, constraint \`student_allocation_school_code_foreign\` foreign key(\`school_code\`) references \`school\`(\`school_code\`) on update cascade, constraint \`student_allocation_school_code_academic_year_class_code_foreign\` foreign key(\`school_code\`, \`academic_year\`, \`class_code\`) references \`class\`(\`school_code\`, \`academic_year\`, \`class_code\`) on delete set null on update cascade, primary key (\`student_id\`, \`academic_year\`)); | ||
create index \`student_allocation_school_code_index\` on \`student_allocation\` (\`school_code\`); | ||
create index \`student_allocation_school_code_academic_year_class_code_index\` on \`student_allocation\` (\`school_code\`, \`academic_year\`, \`class_code\`); | ||
pragma foreign_keys = on; | ||
" | ||
`; | ||
|
||
exports[`GH issue 4478 2`] = ` | ||
[ | ||
[ | ||
"[query] begin", | ||
], | ||
[ | ||
"[query] insert into \`school\` (\`school_code\`) values ('abc') returning \`school_code\`", | ||
], | ||
[ | ||
"[query] insert into \`class\` (\`school_code\`, \`academic_year\`, \`class_code\`) values ('abc', '2023', 'cls') returning \`school_code\`, \`academic_year\`, \`class_code\`", | ||
], | ||
[ | ||
"[query] insert into \`student_allocation\` (\`student_id\`, \`academic_year\`, \`school_code\`, \`class_code\`) values ('1', '2023', 'abc', NULL), ('2', '2023', 'abc', 'cls') returning \`student_id\`, \`academic_year\`", | ||
], | ||
[ | ||
"[query] commit", | ||
], | ||
] | ||
`; |