On Sun, Jul 10, 2011 at 5:35 AM, Jim D. <jim.dal...@gmail.com> wrote:
> The problem is that by default in most of our backends, a transaction runs > with constraint checks deferred. This means that foreign keys are *not* > checked for integrity until a transaction is committed. But here's the rub: > we never commit a transaction during tests, we roll it back instead. > Therefore, none of the queries against the DB are being checked for > integrity. (The exception is MySQL InnoDB, which I'll get to in a sec.) > As part of the work I was doing recently to disable and re-enable constraint > checks during fixture loading, I realized that we were handling this sort of > incorrectly across our backends. Postgresql in fact does provide a facility > for enabling constraint checks: SET CONSTRAINT CHECKS ALL IMMEDIATE. This > statement causes Postgresql to behave as it normally would, when it was > executing queries outside of a transaction. By default, PostgreSQL creates all constraints as NOT DEFERRABLE. To get the observed behaviour we'd need to be explicitly defining constraints as DEFERRABLE INITIALLY DEFERRED. Maintaining the property of deferrable constraints seems important here, so changing the deferrability of constraints, or overriding it using the SET CONSTRAINTS command at the top of the transaction might not be what we want. What I would recommend is that we issue an explicit SET CONSTRAINTS ALL IMMEDIATE command immediately before the ROLLBACK at *end* of test. This will fire any outstanding checks. That way all constraint checks will occur in the same place they would during a commit, yet we can maintain the situation that the test ends with a rollback. -- Simon Riggs http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.