Opened 10 years ago

Closed 10 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: dev
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

Description

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/schema.py

    diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py
    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 by fallen_flint, 10 years ago

Type: UncategorizedBug

comment:2 by Baptiste Mispelon, 10 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

Hi,

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.

Thanks.

comment:3 by Claude Paroz, 10 years ago

Component: Database layer (models, ORM)Migrations

comment:4 by Baptiste Mispelon, 10 years ago

Needs tests: unset

Tentative pull request (with tests) here: https://github.com/django/django/pull/2378

Reviews welcome.

comment:5 by Tim Graham, 10 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Baptiste Mispelon <bmispelon@…>, 10 years ago

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