Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(postgres): allow defining deferred FK constraints (#5384)
This PR adds the option to specify constraints as deferrable for relations. On OneToOne and ManyToOne relations you can now specify a `deferMode` property, which can be either `not deferrable` (default), `immediate` or `deferred`. Those are also covered by a new `DeferMode` enum. Closes #5306 --------- Co-authored-by: Martin Adámek <banan23@gmail.com>
- Loading branch information
Showing
13 changed files
with
191 additions
and
7 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
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
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
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
52 changes: 52 additions & 0 deletions
52
tests/features/deferrable-constraints/deferrable-constraints.postgres.test.ts
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,52 @@ | ||
import { DeferMode, Entity, ManyToOne, PrimaryKey, MikroORM, Ref, Reference } from '@mikro-orm/postgresql'; | ||
|
||
@Entity() | ||
class Parent { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
} | ||
|
||
@Entity() | ||
class Child { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@ManyToOne(() => Parent, { ref: true, deferMode: DeferMode.INITIALLY_DEFERRED }) | ||
parent!: Ref<Parent>; | ||
|
||
} | ||
|
||
describe('deferrable constraints in postgres', () => { | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [Parent, Child], | ||
dbName: `mikro_orm_test_deferrable`, | ||
}); | ||
await orm.schema.refreshDatabase(); | ||
}); | ||
beforeEach(async () => orm.schema.clearDatabase()); | ||
afterAll(async () => { | ||
await orm.schema.dropDatabase(); | ||
await orm.close(true); | ||
}); | ||
|
||
test('insert deferred', async () => { | ||
await orm.em.transactional(async em => { | ||
const parent = new Parent(); | ||
parent.id = 1; | ||
const child = new Child(); | ||
child.id = 1; | ||
child.parent = Reference.createFromPK(Parent, 1); | ||
|
||
await em.persistAndFlush(child); | ||
await em.persistAndFlush(parent); | ||
}); | ||
}); | ||
|
||
}); |
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