On Tuesday, December 22, 2015 at 1:20:23 AM UTC+2, Shai Berger wrote: > > > We could add a documented API to check wanted models for foreign key > > constraint violations (.check_constraints(Model1, Model2) would be a > > likely API). Then lets just document that depending on the database > > this check can be slow. On some databases we can get away with SET > > CONSTRAINTS IMMEDIATE > > [http://www.postgresql.org/docs/9.1/static/sql-set-constraints.html], > > which should be fast. > > Then why don't we implement check_constraints() as > "SET CONSTRAINTS IMMEDIATE; SET CONSTRAINTS DEFERRED" on PG? That is > documented to only check outstanding changes in the transaction. > > I am not sure about the performance implications on Oracle, but the Oracle > docs suggest "Making constraints immediate at the end of a transaction is > a > way of checking whether COMMIT can succeed."[1] > > As far as I know, neither MySql nor Sqlite support deferrable constraints. >
Hmmh, if we only need to check deferred constraints, this can be made to work. I don't believe SET CONSTRAINTS is expensive at all, it does the same job the DB needs to do at commit time in any case. Using SET CONSTRAINTS ALL IMMEDIATE; SET CONSTRAINTS ALL DEFERRED; is a bit problematic, as any constraint that was DEFERRABLE INITIALLY IMMEDIATE would then be set to deferred mode after the commands. This isn't a big problem in practice, unless we at some point allow creating DEFERRABLE INITIALLY IMMEDIATE constraints. We could also explicitly name all the constraints we want to defer, but I'm not sure if we actually reliably know the names of the constraints. So, we could add a flag "check_deferred_constraints" to atomic(), and use the above SET CONSTRAINTS commands to achieve the wanted results. We will likely need to document that DEFERRABLE INITIALLY IMMEDIATE constraints will be set to deferred mode. - Anssi -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/ecb654fc-42cc-4310-a849-a320e42bf0b7%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
