#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

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 Changed 18 months ago by fallen_flint

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Type changed from Uncategorized to Bug

comment:2 Changed 18 months ago by bmispelon

  • Needs tests set
  • Triage Stage changed from Unreviewed to Accepted

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 Changed 18 months ago by claudep

  • Component changed from Database layer (models, ORM) to Migrations

comment:4 Changed 18 months ago by bmispelon

  • Needs tests unset

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

Reviews welcome.

comment:5 Changed 18 months ago by timo

  • Triage Stage changed from Accepted to Ready for checkin

comment:6 Changed 18 months ago by Baptiste Mispelon <bmispelon@…>

  • Resolution set to fixed
  • Status changed from new to closed

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