[Django] #26117: Apps with a routers.py file using multiple databases may not use --fake-initial

37 views
Skip to first unread message

Django

unread,
Jan 21, 2016, 7:30:46 PM1/21/16
to django-...@googlegroups.com
#26117: Apps with a routers.py file using multiple databases may not use --fake-
initial
-----------------------------+--------------------
Reporter: scottsexton | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------
In the case where you have an app that defines a routers.py to associate
certain models with a different database, the initial migration for that
app cannot be faked with --fake-initial. Because some of the models exist
in a different database than the one Django is currently migrating, it
will ignore the fake and try to apply the migration anyway, giving you an
OperationalError that one of the tables already exists.

The documentation reveals the location of the bug: "For an initial
migration that creates one or more tables (CreateModel operation), Django
checks that all of those tables already exist in the database and fake-
applies the migration if so." But does it check the right database for
those tables? No.

The bug occurs occurs in django.db.migrations.executor.py in the
detect_soft_applied method. It simply compares all models it finds to a
list of tables for the current db `existing_table_names =
self.connection.introspection.table_names(self.connection.cursor())`,
however, if a certain model exists in a different DB as defined in the
app's routers.py, this check will always fail.

Instead, this method should probably consider the return value of the
allow_migrate() method in the app's routers.py before trying to confirm
whether that model should exist in the current db.

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

Django

unread,
Jan 22, 2016, 8:30:00 AM1/22/16
to django-...@googlegroups.com
#26117: Add database routers support to initial migration detection
-----------------------------+------------------------------------

Reporter: scottsexton | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
* stage: Unreviewed => Accepted


Comment:

Thanks for the detailed report. Feel free to offer a patch if you are
able.

--
Ticket URL: <https://code.djangoproject.com/ticket/26117#comment:1>

Django

unread,
Jan 22, 2016, 12:05:22 PM1/22/16
to django-...@googlegroups.com
#26117: Add database routers support to initial migration detection
-----------------------------+---------------------------------------
Reporter: scottsexton | Owner: scottsexton
Type: Bug | Status: assigned
Component: Migrations | Version: 1.9

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+---------------------------------------
Changes (by scottsexton):

* status: new => assigned
* owner: nobody => scottsexton


Comment:

I'll submit a pull request this weekend.

--
Ticket URL: <https://code.djangoproject.com/ticket/26117#comment:2>

Django

unread,
Jan 29, 2016, 10:32:28 AM1/29/16
to django-...@googlegroups.com
#26117: Add database routers support to initial migration detection
-----------------------------+---------------------------------------
Reporter: scottsexton | Owner: scottsexton
Type: Bug | Status: assigned
Component: Migrations | Version: 1.9

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+---------------------------------------
Changes (by timgraham):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/6036 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/26117#comment:3>

Django

unread,
Feb 24, 2016, 1:46:58 PM2/24/16
to django-...@googlegroups.com
#26117: Add database routers support to initial migration detection
-----------------------------+---------------------------------------
Reporter: scottsexton | Owner: scottsexton
Type: Bug | Status: assigned
Component: Migrations | Version: 1.9

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-----------------------------+---------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1


Comment:

Comments for improvement on the pull request.

--
Ticket URL: <https://code.djangoproject.com/ticket/26117#comment:4>

Django

unread,
Feb 25, 2016, 10:11:46 AM2/25/16
to django-...@googlegroups.com
#26117: Add database routers support to initial migration detection
-----------------------------+---------------------------------------
Reporter: scottsexton | Owner: scottsexton
Type: Bug | Status: closed
Component: Migrations | Version: 1.9
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-----------------------------+---------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"fc584f06859b88474ea0db49bfe142e87428865d" fc584f0]:
{{{
#!CommitTicketReference repository=""
revision="fc584f06859b88474ea0db49bfe142e87428865d"
Fixed #26117 -- Consulted database routers in initial migration detection.

Thanks Simon Charette for help.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26117#comment:5>

Reply all
Reply to author
Forward
0 new messages