#30108: Include FK constraints when adding columns in PostgreSQL
-------------------------------------+-------------------------------------
     Reporter:  Dan Tao              |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Migrations           |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Dan Tao):

 I've taken a look at the code already, and I believe it's ''almost''
 there.

 The `BaseDatabaseSchemaEditor` class includes the attribute
 `sql_create_inline_fk`, `None` by default, which is appended to a column
 definition when creating a table. It is not included when adding a field,
 but as far as I can tell that is simply an oversight.

 Somewhat surprisingly, this attribute is ''only'' defined on the sqlite3
 backend. (Why there was any need to optimize this particular backend, I
 couldn't say.)

 I attempted to make the following changes:

 1. Update the logic in `BaseDatabaseSchemaEditor.add_field` to append
 `sql_create_inline_fk` (if defined) to the column definition.
 2. Add the `sql_create_inline_fk` attribute to the postgresql backend
 class.

 Unfortunately this ended up causing test failures because there are
 
[https://github.com/django/django/blob/master/tests/mutually_referential/models.py
 test models] defined with a cyclic dependency. It seems that SQLite
 doesn't mind if you create a table with a foreign key to another table
 that doesn't exist yet, but PostgreSQL does.

 {{{
 psycopg2.ProgrammingError: relation "mutually_referential_child" does not
 exist
 }}}

 I'm not sure what the solution is. Perhaps the code could be refactored so
 that the postgresql backend ''only'' applies the `sql_create_inline_fk`
 optimization in `add_field` (which is when this scenario can occur) and
 not in `create_model` (when it shouldn't matter, because a table scan on
 an empty table is not problematic).

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30108#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" 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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.48c5884b1b0c1bd152f921947f20eaab%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to