[Django] #28789: Django won't update sqlite_master table using migrations.AlterModelTable, which causes loaddata to fail.

29 views
Skip to first unread message

Django

unread,
Nov 9, 2017, 6:48:51 AM11/9/17
to django-...@googlegroups.com
#28789: Django won't update sqlite_master table using migrations.AlterModelTable,
which causes loaddata to fail.
-------------------------------------+-------------------------------------
Reporter: Rafael | Owner: nobody
Pires |
Type: Bug | Status: new
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords: migrations, sqlite3
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I created a migration to rename a model table:


{{{
class Migration(migrations.Migration):

dependencies = [
('assessment', '0105_previous_migration'),
]

operations = [
migrations.AlterModelTable(name='MyModel',
table='myapp_newmymodel')
]

}}}

I can run this migration just fine, the table gets renamed and the project
runs fine when I access it through my browser (with SQLite3 and MySQL).

The problem is that: then I run "./manage.py test" the test fails to load
the fixtures in the SQLite3 database:


{{{
./manage.py test assessment.tests.test_models
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E
======================================================================
ERROR: setUpClass (assessment.tests.test_models.ModelsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/user/virtualenv/myproject/local/lib/python2.7/site-
packages/django/test/testcases.py", line 1036, in setUpClass
'database': db_name,
File "/home/user/virtualenv/myproject/local/lib/python2.7/site-
packages/django/core/management/__init__.py", line 131, in call_command
return command.execute(*args, **defaults)
File "/home/user/virtualenv/myproject/local/lib/python2.7/site-
packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/user/virtualenv/myproject/local/lib/python2.7/site-
packages/django/core/management/commands/loaddata.py", line 69, in handle
self.loaddata(fixture_labels)
File "/home/user/virtualenv/myproject/local/lib/python2.7/site-
packages/django/core/management/commands/loaddata.py", line 115, in
loaddata
connection.check_constraints(table_names=table_names)
File "/home/user/virtualenv/myproject/local/lib/python2.7/site-
packages/django/db/backends/sqlite3/base.py", line 286, in
check_constraints
column_name, referenced_column_name,
File "/home/user/virtualenv/myproject/local/lib/python2.7/site-
packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/user/virtualenv/myproject/local/lib/python2.7/site-
packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/user/virtualenv/myproject/local/lib/python2.7/site-
packages/django/db/backends/utils.py", line 63, in execute
return self.cursor.execute(sql)
File "/home/user/virtualenv/myproject/local/lib/python2.7/site-
packages/django/db/backends/sqlite3/base.py", line 326, in execute
return Database.Cursor.execute(self, query)
OperationalError: Problem installing fixtures: no such table:
myapp_mymodel
}}}

*This table id is used by other tables as Foreign Key.*

I looked into the code and it seems that the
**migrations.AlterModelTable** is not updating the sqlite_master table, so
other tables that references my new table with a Foreign Key are still
pointing to myapp_mymodel instead of myapp_newmymodel. This happens during
**check_constraints** execution, the sqlite3 backend gets the key columns
using **get_key_columns** function, that reads the "CREATE TABLE" query in
sqlite_master, but since the sqlite_master table wasn't updated correctly,
the process fails.

Just to be sure I ran "ALTER TABLE myapp_mymodel RENAME TO
myapp_newmymodel" directly on the database and the sqlite_master table is
updated correctly.

Database: SQLite3
Django version: 1.11.5

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

Django

unread,
Nov 9, 2017, 7:00:24 AM11/9/17
to django-...@googlegroups.com
#28789: Django won't update sqlite_master table using migrations.AlterModelTable,
which causes loaddata to fail.
-------------------------------------+-------------------------------------
Reporter: Rafael Pires | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:

Keywords: migrations, sqlite3 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Rafael Pires:

Old description:

New description:


{{{
class Migration(migrations.Migration):

}}}

I also ran **cursor.execute("ALTER TABLE myapp_mymodel RENAME TO
myapp_newmymodel")** instead of **migrations.AlterModelTable** in the
migration, but the problem is the same: sqlite3 backend won't update
sqlite_master references.

Database: SQLite3
Django version: 1.11.5

--

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

Django

unread,
Nov 9, 2017, 9:37:13 AM11/9/17
to django-...@googlegroups.com
#28789: Django won't update sqlite_master table using migrations.AlterModelTable,
which causes loaddata to fail.
-------------------------------------+-------------------------------------
Reporter: Rafael Pires | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.11
Severity: Normal | Resolution:

Keywords: migrations, sqlite3 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* component: Database layer (models, ORM) => Migrations


Comment:

I don't see an obvious place where Django is at fault. Can you provide a
sample project that reproduces this problem?

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

Django

unread,
Nov 14, 2017, 2:10:56 PM11/14/17
to django-...@googlegroups.com
#28789: Django won't update sqlite_master table using migrations.AlterModelTable,
which causes loaddata to fail.
-------------------------------------+-------------------------------------
Reporter: Rafael Pires | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.11
Severity: Normal | Resolution: needsinfo

Keywords: migrations, sqlite3 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => needsinfo


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

Django

unread,
Nov 30, 2017, 11:08:48 AM11/30/17
to django-...@googlegroups.com
#28789: Django won't update sqlite_master table using migrations.AlterModelTable,
which causes loaddata to fail.
-------------------------------------+-------------------------------------
Reporter: Rafael Pires | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.11
Severity: Normal | Resolution: duplicate

Keywords: migrations, sqlite3 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* resolution: needsinfo => duplicate


Comment:

I think this is a duplicate of #28849.

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

Django

unread,
May 8, 2019, 10:48:20 AM5/8/19
to django-...@googlegroups.com
#28789: Django won't update sqlite_master table using migrations.AlterModelTable,
which causes loaddata to fail.
-------------------------------------+-------------------------------------
Reporter: Rafael Pires | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.11
Severity: Normal | Resolution: duplicate
Keywords: migrations, sqlite3 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by MarcoGlauser):

We ran into the same problem with django 1.11. The fix hasn't been
backported to 1.11 and I don't think it will be backported any time soon.
After some trial and error, we found a workaround:

We copy the original Foreignkey definition and append it to the migration
that contains the AlterModelTable as a AlterField. This will update the
Foreignkey in SQLite.

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

Reply all
Reply to author
Forward
0 new messages