#30664: SQLite3 migration can fail when used quoted db_table in models
-------------------------------------+-------------------------------------
     Reporter:  Maciej Olko          |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Migrations           |                  Version:  2.2
     Severity:  Normal               |               Resolution:
     Keywords:  quoted, db_table,    |             Triage Stage:
  meta, model, sqlite3, syntax-      |  Unreviewed
  error                              |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Maciej Olko:

Old description:

> If model's Meta db_table is quoted, e.g. '"table_with_quoted_name"',
> SQLite3 migration with this table creation with can fail with
> django.db.utils.OperationalError: near "table_with_quoted_name": syntax
> error.
>
> I suppose following generated query causes the error:
>
> CREATE TABLE "new__"table_with_quoted_name"" ("obj_id" integer NOT NULL
> PRIMARY KEY, "obj_num" varchar(20) NULL,  "country_id" integer NOT NULL
> REFERENCES "countries" ("country_id") DEFERRABLE INITIALLY DEFERRED)
>
> To reproduce table with quoted name should have at least one foreign key.
>
> Django documentation says it supports quoted names
> (https://docs.djangoproject.com/en/2.2/ref/databases/#naming-issues).
> > Quoted names can also be used with Django’s other supported database
> backends; except for Oracle, however, the quotes have no effect.
>
> Traceback:
>
> Traceback (most recent call last):
>   File "…/django/db/backends/utils.py", line 82, in _execute
>     return self.cursor.execute(sql)
>   File "…/django/db/backends/sqlite3/base.py", line 382, in execute
>     return Database.Cursor.execute(self, query)
> sqlite3.OperationalError: near "table_with_quoted_name": syntax error
>
> The above exception was the direct cause of the following exception:
>
> Traceback (most recent call last):
>   File "./manage.py", line 15, in <module>
>     execute_from_command_line(sys.argv)
>   …
>   File "…/django/core/management/commands/migrate.py", line 234, in
> handle
>     fake_initial=fake_initial,
>   File "…/django/db/migrations/executor.py", line 117, in migrate
>     state = self._migrate_all_forwards(state, plan, full_plan, fake=fake,
> fake_initial=fake_initial)
>   File "…/django/db/migrations/executor.py", line 147, in
> _migrate_all_forwards
>     state = self.apply_migration(state, migration, fake=fake,
> fake_initial=fake_initial)
>   File "…/django/db/migrations/executor.py", line 245, in apply_migration
>     state = migration.apply(state, schema_editor)
>   File "…/django/db/migrations/migration.py", line 124, in apply
>     operation.database_forwards(self.app_label, schema_editor, old_state,
> project_state)
>   File "…/django/db/migrations/operations/fields.py", line 112, in
> database_forwards
>     field,
>   File "…/django/db/backends/sqlite3/schema.py", line 327, in add_field
>     self._remake_table(model, create_field=field)
>   File "…/django/db/backends/sqlite3/schema.py", line 279, in
> _remake_table
>     self.create_model(new_model)
>   File "…/django/db/backends/base/schema.py", line 307, in create_model
>     self.execute(sql, params or None)
>   File "…/django/db/backends/base/schema.py", line 137, in execute
>     cursor.execute(sql, params)
>   File "…/django/db/backends/utils.py", line 99, in execute
>     return super().execute(sql, params)
>   File "…/django/db/backends/utils.py", line 67, in execute
>     return self._execute_with_wrappers(sql, params, many=False,
> executor=self._execute)
>   File "…/django/db/backends/utils.py", line 76, in
> _execute_with_wrappers
>     return executor(sql, params, many, context)
>   File "…/django/db/backends/utils.py", line 84, in _execute
>     return self.cursor.execute(sql, params)
>   File "…/django/db/utils.py", line 89, in __exit__
>     raise dj_exc_value.with_traceback(traceback) from exc_value
>   File "…/django/db/backends/utils.py", line 82, in _execute
>     return self.cursor.execute(sql)
>   File "…/django/db/backends/sqlite3/base.py", line 382, in execute
>     return Database.Cursor.execute(self, query)
> django.db.utils.OperationalError: near "table_with_quoted_name": syntax
> error

New description:

 If model's Meta db_table is quoted, e.g. '"table_with_quoted_name"',
 SQLite3 migration with this table creation with can fail with
 django.db.utils.OperationalError: near "table_with_quoted_name": syntax
 error.

 I suppose following generated query causes the error:
 {{{
 CREATE TABLE "new__"table_with_quoted_name"" ("obj_id" integer NOT NULL
 PRIMARY KEY, "obj_num" varchar(20) NULL,  "country_id" integer NOT NULL
 REFERENCES "countries" ("country_id") DEFERRABLE INITIALLY DEFERRED)
 }}}
 To reproduce table with quoted name should have at least one foreign key.

 Django documentation says it supports quoted names
 (https://docs.djangoproject.com/en/2.2/ref/databases/#naming-issues).
 > Quoted names can also be used with Django’s other supported database
 backends; except for Oracle, however, the quotes have no effect.

 Traceback:
 {{{
 Traceback (most recent call last):
   File "…/django/db/backends/utils.py", line 82, in _execute
     return self.cursor.execute(sql)
   File "…/django/db/backends/sqlite3/base.py", line 382, in execute
     return Database.Cursor.execute(self, query)
 sqlite3.OperationalError: near "table_with_quoted_name": syntax error

 The above exception was the direct cause of the following exception:

 Traceback (most recent call last):
   File "./manage.py", line 15, in <module>
     execute_from_command_line(sys.argv)
   …
   File "…/django/core/management/commands/migrate.py", line 234, in handle
     fake_initial=fake_initial,
   File "…/django/db/migrations/executor.py", line 117, in migrate
     state = self._migrate_all_forwards(state, plan, full_plan, fake=fake,
 fake_initial=fake_initial)
   File "…/django/db/migrations/executor.py", line 147, in
 _migrate_all_forwards
     state = self.apply_migration(state, migration, fake=fake,
 fake_initial=fake_initial)
   File "…/django/db/migrations/executor.py", line 245, in apply_migration
     state = migration.apply(state, schema_editor)
   File "…/django/db/migrations/migration.py", line 124, in apply
     operation.database_forwards(self.app_label, schema_editor, old_state,
 project_state)
   File "…/django/db/migrations/operations/fields.py", line 112, in
 database_forwards
     field,
   File "…/django/db/backends/sqlite3/schema.py", line 327, in add_field
     self._remake_table(model, create_field=field)
   File "…/django/db/backends/sqlite3/schema.py", line 279, in
 _remake_table
     self.create_model(new_model)
   File "…/django/db/backends/base/schema.py", line 307, in create_model
     self.execute(sql, params or None)
   File "…/django/db/backends/base/schema.py", line 137, in execute
     cursor.execute(sql, params)
   File "…/django/db/backends/utils.py", line 99, in execute
     return super().execute(sql, params)
   File "…/django/db/backends/utils.py", line 67, in execute
     return self._execute_with_wrappers(sql, params, many=False,
 executor=self._execute)
   File "…/django/db/backends/utils.py", line 76, in _execute_with_wrappers
     return executor(sql, params, many, context)
   File "…/django/db/backends/utils.py", line 84, in _execute
     return self.cursor.execute(sql, params)
   File "…/django/db/utils.py", line 89, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File "…/django/db/backends/utils.py", line 82, in _execute
     return self.cursor.execute(sql)
   File "…/django/db/backends/sqlite3/base.py", line 382, in execute
     return Database.Cursor.execute(self, query)
 django.db.utils.OperationalError: near "table_with_quoted_name": syntax
 error
 }}}

--

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.325cb1828b979a6cd56022e6c37377cd%40djangoproject.com.

Reply via email to