[Django] #23702: sqlite3 schema editor fails when altering a model's pk

6 views
Skip to first unread message

Django

unread,
Oct 22, 2014, 1:28:25 PM10/22/14
to django-...@googlegroups.com
#23702: sqlite3 schema editor fails when altering a model's pk
----------------------------------------------+--------------------
Reporter: gavinwahl | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer (models, ORM) | Version: 1.7
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
I changed the pk in my model from the implicit one to a custom one -- `id
= models.BigIntegerField(primary_key=True)` -- and generated a migration:

{{{#!python
operations = [
migrations.AlterField(
model_name='foo',
name='id',
field=models.BigIntegerField(serialize=False,
primary_key=True),
),
]
}}}

This migration fails to run on sqlite:

{{{
Traceback (most recent call last):
File "./manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "/srv/python-environments/sb/local/lib/python2.7/site-
packages/django/core/management/__init__.py", line 385, in
execute_from_command_line
utility.execute()
File "/srv/python-environments/sb/local/lib/python2.7/site-
packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/srv/python-environments/sb/local/lib/python2.7/site-
packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/srv/python-environments/sb/local/lib/python2.7/site-
packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/srv/python-environments/sb/local/lib/python2.7/site-
packages/django/core/management/commands/migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/srv/python-environments/sb/local/lib/python2.7/site-
packages/django/db/migrations/executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
File "/srv/python-environments/sb/local/lib/python2.7/site-
packages/django/db/migrations/executor.py", line 97, in apply_migration
migration.apply(project_state, schema_editor)
File "/srv/python-environments/sb/local/lib/python2.7/site-
packages/django/db/migrations/migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor,
project_state, new_state)
File "/srv/python-environments/sb/local/lib/python2.7/site-
packages/django/db/migrations/operations/fields.py", line 139, in
database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/srv/python-environments/sb/local/lib/python2.7/site-
packages/django/db/backends/schema.py", line 473, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type,
old_db_params, new_db_params, strict)
File "/srv/python-environments/sb/local/lib/python2.7/site-
packages/django/db/backends/sqlite3/schema.py", line 190, in _alter_field
self._remake_table(model, alter_fields=[(old_field, new_field)])
File "/srv/python-environments/sb/local/lib/python2.7/site-
packages/django/db/backends/sqlite3/schema.py", line 78, in _remake_table
del body[old_field.name]
KeyError: u'id'
}}}


Changing

{{{#!python
del body[old_field.name]
del mapping[old_field.column]
}}}

To

{{{#!python
body.pop(old_field.name, None)
mapping.pop(old_field.column], None)
}}}

fixes the issue and the migration runs.

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

Django

unread,
Oct 23, 2014, 10:47:14 AM10/23/14
to django-...@googlegroups.com
#23702: sqlite3 schema editor fails when altering a model's pk
-------------------------------------+-------------------------------------
Reporter: gavinwahl | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) | Resolution:
Severity: Release blocker | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timgraham):

* severity: Normal => Release blocker
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted


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

Django

unread,
Oct 23, 2014, 11:45:35 AM10/23/14
to django-...@googlegroups.com
#23702: sqlite3 schema editor fails when altering a model's pk
-------------------------------------+-------------------------------------
Reporter: gavinwahl | Owner: timgraham
Type: Bug | Status: assigned

Component: Database layer | Version: 1.7
(models, ORM) | Resolution:
Severity: Release blocker | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timgraham):

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


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

Django

unread,
Oct 23, 2014, 12:40:07 PM10/23/14
to django-...@googlegroups.com
#23702: sqlite3 schema editor fails when altering a model's pk
-------------------------------------+-------------------------------------
Reporter: gavinwahl | Owner: timgraham
Type: Bug | Status: assigned
Component: Database layer | Version: 1.7
(models, ORM) | Resolution:
Severity: Release blocker | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0

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

* has_patch: 0 => 1


Comment:

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

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

Django

unread,
Oct 23, 2014, 2:26:53 PM10/23/14
to django-...@googlegroups.com
#23702: sqlite3 schema editor fails when altering a model's pk
-------------------------------------+-------------------------------------
Reporter: gavinwahl | Owner: timgraham
Type: Bug | Status: assigned
Component: Database layer | Version: 1.7
(models, ORM) | Resolution:
Severity: Release blocker | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* stage: Accepted => Ready for checkin


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

Django

unread,
Oct 23, 2014, 2:35:50 PM10/23/14
to django-...@googlegroups.com
#23702: sqlite3 schema editor fails when altering a model's pk
-------------------------------------+-------------------------------------
Reporter: gavinwahl | Owner: timgraham
Type: Bug | Status: closed

Component: Database layer | Version: 1.7
(models, ORM) | Resolution: fixed

Severity: Release blocker | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

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


Comment:

In [changeset:"92269b7b53c9f69dd28e119c40b6bf32f96f7bd3"]:
{{{
#!CommitTicketReference repository=""
revision="92269b7b53c9f69dd28e119c40b6bf32f96f7bd3"
Fixed #23702 -- Fixed adding an explicit id field on SQLite.

Thanks gavinwahl for the report.
}}}

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

Django

unread,
Oct 23, 2014, 2:37:18 PM10/23/14
to django-...@googlegroups.com
#23702: sqlite3 schema editor fails when altering a model's pk
-------------------------------------+-------------------------------------
Reporter: gavinwahl | Owner: timgraham
Type: Bug | Status: closed
Component: Database layer | Version: 1.7
(models, ORM) | Resolution: fixed
Severity: Release blocker | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"7750fc8fa80470e4c10b3753667e04f78ebe4781"]:
{{{
#!CommitTicketReference repository=""
revision="7750fc8fa80470e4c10b3753667e04f78ebe4781"
[1.7.x] Fixed #23702 -- Fixed adding an explicit id field on SQLite.

Thanks gavinwahl for the report.

Backport of 92269b7b53 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23702#comment:6>

Reply all
Reply to author
Forward
0 new messages