You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
I'm always frustrated when I try to insert a lot of data in a single transaction and the foreign key constraints are violated because the related row was not inserted yet in that statement, but will be inserted later as part of the same transaction. SQL has a solution for this: deferring constraint checks until the commit of the transaction.
Describe the solution you'd like
It would be nice if we can set deferrablility and default check timing through the ORM, maybe as options in the relationship decorators (@ManyToOne, @OneToOne, etc).
this.addSql('create table "entity_b" ("id" serial primary key, "entity_a_id" int not null);');this.addSql('alter table "entity_b" add constraint "entity_b_entity_a_id_foreign" foreign key ("entity_a_id") references "entity_a" ("id") on update cascade deferrable initially deferred;');
And we could have a default global value for these options. I don't know if there's a place for driver-specific configs, or maybe we could put them with the orm options.
Also for the transaction, maybe we could have an option for the check timing in em.begin() or em.transactional():
awaitem.begin({checkTiming: "DEFERRED"})
Describe alternatives you've considered
I could manually edit the migration files to make constraints deferrable, and run a raw query to SET CONSTRAINTS DEFERRED
Or alternatively I could insert my data in an order that guarentees no conflicts even within the same transaction.
Additional context
Somewhat related issue #1175 because I saw a test for immediate constraints in the issue tests.
The text was updated successfully, but these errors were encountered:
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>
Is your feature request related to a problem? Please describe.
I'm always frustrated when I try to insert a lot of data in a single transaction and the foreign key constraints are violated because the related row was not inserted yet in that statement, but will be inserted later as part of the same transaction. SQL has a solution for this: deferring constraint checks until the commit of the transaction.
In postgres this is done by setting the constraint to be DEFERRABLE, and setting the constraint check timing for the transaction to DEFERRED. Or just setting the default behavior of the constraint to be DEFERRABLE INITIALLY DEFERRED to apply by default for all transactions.
Describe the solution you'd like
It would be nice if we can set deferrablility and default check timing through the ORM, maybe as options in the relationship decorators (
@ManyToOne
,@OneToOne
, etc).For example:
Then the generated migration would be:
And we could have a default global value for these options. I don't know if there's a place for driver-specific configs, or maybe we could put them with the orm options.
Also for the transaction, maybe we could have an option for the check timing in
em.begin()
orem.transactional()
:Describe alternatives you've considered
I could manually edit the migration files to make constraints deferrable, and run a raw query to
SET CONSTRAINTS DEFERRED
Or alternatively I could insert my data in an order that guarentees no conflicts even within the same transaction.
Additional context
Somewhat related issue #1175 because I saw a test for immediate constraints in the issue tests.
The text was updated successfully, but these errors were encountered: