Opened 4 years ago

Closed 4 years ago

#22168 closed Bug (fixed)

Sqlite3 backend fails during _remake_table with column named "order"

Reported by: netwamp@… Owned by: nobody
Component: Migrations Version: master
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: yes UI/UX: no


It seems that column names should be quoted, cause if I try migrate my app, migration fails on following sql request:

INSERT INTO "services_group__new" (description, order, id, slug, title) SELECT description, order, id, slug, title FROM "services_group"

If I try it in db shell, it fails too, as expected.
Message is "Error: near "order": syntax error"

Adding quotes around column names in django.db.backends.sqlite3.schema.DatabaseSchemaEditor._remake_table fixes the problem for me:

  • django/db/backends/sqlite3/

    diff --git a/django/db/backends/sqlite3/ b/django/db/backends/sqlite3/
    index 279ac4b..e8fc263 100644
    a b class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): 
    8383        field_maps = list(mapping.items())
    8484        self.execute("INSERT INTO %s (%s) SELECT %s FROM %s" % (
    8585            self.quote_name(temp_model._meta.db_table),
    86             ', '.join(x for x, y in field_maps),
    87             ', '.join(y for x, y in field_maps),
     86            ', '.join(self.quote_name(x) for x, y in field_maps),
     87            ', '.join(self.quote_name(y) for x, y in field_maps),
    8888            self.quote_name(model._meta.db_table),
    8989        ))
    9090        # Delete the old table

Change History (6)

comment:1 Changed 4 years ago by fallen_flint

Type: UncategorizedBug

comment:2 Changed 4 years ago by Baptiste Mispelon

Needs tests: set
Triage Stage: UnreviewedAccepted


I can reproduce the issue by doing the following:

1) Create a model with a field called order.
2) Run makemigrations. This creates a first migration file.
3) Add another field to the model created in 1)
4) Run makemigrations again. This creates a second migration file.
5) Attempt to run migrate. This fails with the reported error.

Your proposed fix looks sensible and it works for the reported issue but a full patch should have some tests too.


comment:3 Changed 4 years ago by Claude Paroz

Component: Database layer (models, ORM)Migrations

comment:4 Changed 4 years ago by Baptiste Mispelon

Needs tests: unset

Tentative pull request (with tests) here:

Reviews welcome.

comment:5 Changed 4 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:6 Changed 4 years ago by Baptiste Mispelon <bmispelon@…>

Resolution: fixed
Status: newclosed

In c679cb7f600b13646a1a2b5fc8a03dfcc2e413f2:

Fixed #22168 -- Fixed migrations failing on sqlite when column names are SQL keywords

Thanks to trac user fallen_flint for the report and initial patch.

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