Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#23702 closed Bug (fixed)

sqlite3 schema editor fails when altering a model's pk

Reported by: Gavin Wahl Owned by: Tim Graham
Component: Database layer (models, ORM) Version: 1.7
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


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:

    operations = [
            field=models.BigIntegerField(serialize=False, primary_key=True),

This migration fails to run on sqlite:

Traceback (most recent call last):
  File "./", line 11, in <module>
  File "/srv/python-environments/sb/local/lib/python2.7/site-packages/django/core/management/", line 385, in execute_from_command_line
  File "/srv/python-environments/sb/local/lib/python2.7/site-packages/django/core/management/", line 377, in execute
  File "/srv/python-environments/sb/local/lib/python2.7/site-packages/django/core/management/", 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/", line 338, in execute
    output = self.handle(*args, **options)
  File "/srv/python-environments/sb/local/lib/python2.7/site-packages/django/core/management/commands/", 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/", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "/srv/python-environments/sb/local/lib/python2.7/site-packages/django/db/migrations/", 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/", 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/", 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/", 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/", 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/", line 78, in _remake_table
    del body[]
KeyError: u'id'


            del body[]
            del mapping[old_field.column]


            body.pop(, None)
            mapping.pop(old_field.column], None)

fixes the issue and the migration runs.

Change History (6)

comment:1 by Tim Graham, 10 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 by Tim Graham, 10 years ago

Owner: changed from nobody to Tim Graham
Status: newassigned

comment:3 by Tim Graham, 10 years ago

Has patch: set

comment:4 by Claude Paroz, 10 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 92269b7b53c9f69dd28e119c40b6bf32f96f7bd3:

Fixed #23702 -- Fixed adding an explicit id field on SQLite.

Thanks gavinwahl for the report.

comment:6 by Tim Graham <timograham@…>, 10 years ago

In 7750fc8fa80470e4c10b3753667e04f78ebe4781:

[1.7.x] Fixed #23702 -- Fixed adding an explicit id field on SQLite.

Thanks gavinwahl for the report.

Backport of 92269b7b53 from master

Note: See TracTickets for help on using tickets.
Back to Top