Login

Snippets by pstiasny

Snippet List

Circular reference with Django ORM and Postgres without breaking NOT NULL FK constraints

The recipe uses deferred constraint validation to create circular references across database tables. The example requires Postgres (MySQL doesn't support deferred constraints). To achieve this, the following is required: * Insertions must be performed in a transaction. Foreign key constraints will be validated at the end of the transactions, allowing for insertion of rows with FKs pointing to rows that don't exist yet. * Primary keys need to be generated before insertion. That's what `prefetch_id` does by pulling the next value from the `*_id_seq` sequence.

  • django
  • orm
  • postgres
Read More

pstiasny has posted 1 snippet.