[Django] #29344: RemoveField on model with a single field

36 views
Skip to first unread message

Django

unread,
Apr 20, 2018, 8:39:13 AM4/20/18
to django-...@googlegroups.com
#29344: RemoveField on model with a single field
-----------------------------------------+------------------------
Reporter: Shadi Akiki | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
I came across this case accidentally, and it's probably not a common use-
case, but I'm reporting it anyway.
Also, it may as well be resolved in 2.0, but this was in 1.11.

The following query got generated from one of my migration files

{{{
INSERT INTO "appname_modelname" () SELECT FROM "appname_modelname__old"
}}}

where I replaced my Application name with "appname" and model name with
"modelname".

This query threw an error in sqlite3.

Here is the complete traceback

{{{
Traceback (most recent call last):
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/backends/sqlite3/base.py", line 330, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: near ")": syntax error

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

Traceback (most recent call last):
File "./manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/core/management/__init__.py", line 364, in
execute_from_command_line
utility.execute()
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake,
fake_initial=fake_initial)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/migrations/executor.py", line 145, in
_migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake,
fake_initial=fake_initial)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state,
project_state)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/migrations/operations/fields.py", line 155, in
database_forwards
schema_editor.remove_field(from_model,
from_model._meta.get_field(self.name))
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/backends/sqlite3/schema.py", line 256, in remove_field
self._remake_table(model, delete_field=field)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/backends/sqlite3/schema.py", line 206, in _remake_table
self.quote_name(model._meta.db_table),
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/backends/base/schema.py", line 120, in execute
cursor.execute(sql, params)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5/site-
packages/django/db/backends/sqlite3/base.py", line 330, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: near ")": syntax error
}}}

Walking through the code, I think that in
`django/db/migrations/operations/fields.py` in
`RemoveField.state_forwards`, maybe something like the following is needed

{{{
# check if we're deleting the last field
if len(state.models[app_label, self.model_name_lower].fields) == 1:
raise ValueError("Invalid operation to delete the last field")
}}}

Or maybe instead of generating a `RemoveField`, it should have been a
`RemoveModel`

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

Django

unread,
Apr 20, 2018, 8:39:45 AM4/20/18
to django-...@googlegroups.com
#29344: RemoveField on model with a single field
-------------------------------+--------------------------------------

Reporter: Shadi Akiki | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 1.11
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Shadi Akiki):

* component: Uncategorized => Migrations


--
Ticket URL: <https://code.djangoproject.com/ticket/29344#comment:1>

Django

unread,
Apr 20, 2018, 8:45:58 AM4/20/18
to django-...@googlegroups.com
#29344: RemoveField on model with a single field
-------------------------------+--------------------------------------

Reporter: Shadi Akiki | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Shadi Akiki:

Old description:

> I came across this case accidentally, and it's probably not a common use-
> case, but I'm reporting it anyway.
> Also, it may as well be resolved in 2.0, but this was in 1.11.
>
> The following query got generated from one of my migration files
>
> {{{
> INSERT INTO "appname_modelname" () SELECT FROM "appname_modelname__old"
> }}}
>
> where I replaced my Application name with "appname" and model name with
> "modelname".
>
> This query threw an error in sqlite3.
>
> Here is the complete traceback
>
> {{{
> Traceback (most recent call last):
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/backends/utils.py", line 65, in execute


> return self.cursor.execute(sql, params)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/backends/sqlite3/base.py", line 330, in execute


> return Database.Cursor.execute(self, query, params)
> sqlite3.OperationalError: near ")": syntax error
>
> The above exception was the direct cause of the following exception:
>
> Traceback (most recent call last):
> File "./manage.py", line 22, in <module>
> execute_from_command_line(sys.argv)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/core/management/__init__.py", line 364, in
> execute_from_command_line
> utility.execute()
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/core/management/__init__.py", line 356, in execute
> self.fetch_command(subcommand).run_from_argv(self.argv)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/core/management/base.py", line 283, in


> run_from_argv
> self.execute(*args, **cmd_options)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/core/management/base.py", line 330, in execute


> output = self.handle(*args, **options)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/core/management/commands/migrate.py", line 204, in
> handle
> fake_initial=fake_initial,
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/migrations/executor.py", line 115, in migrate


> state = self._migrate_all_forwards(state, plan, full_plan, fake=fake,
> fake_initial=fake_initial)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/migrations/executor.py", line 145, in


> _migrate_all_forwards
> state = self.apply_migration(state, migration, fake=fake,
> fake_initial=fake_initial)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/migrations/executor.py", line 244, in


> apply_migration
> state = migration.apply(state, schema_editor)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/migrations/migration.py", line 129, in apply


> operation.database_forwards(self.app_label, schema_editor, old_state,
> project_state)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/migrations/operations/fields.py", line 155, in


> database_forwards
> schema_editor.remove_field(from_model,
> from_model._meta.get_field(self.name))
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/backends/sqlite3/schema.py", line 256, in


> remove_field
> self._remake_table(model, delete_field=field)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/db/backends/sqlite3/schema.py", line 206, in
> _remake_table
> self.quote_name(model._meta.db_table),
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/backends/base/schema.py", line 120, in execute
> cursor.execute(sql, params)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/db/backends/utils.py", line 80, in execute
> return super(CursorDebugWrapper, self).execute(sql, params)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/db/backends/utils.py", line 65, in execute
> return self.cursor.execute(sql, params)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/db/utils.py", line 94, in __exit__


> six.reraise(dj_exc_type, dj_exc_value, traceback)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/utils/six.py", line 685, in reraise
> raise value.with_traceback(tb)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/db/backends/utils.py", line 65, in execute


> return self.cursor.execute(sql, params)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/backends/sqlite3/base.py", line 330, in execute


> return Database.Cursor.execute(self, query, params)
> django.db.utils.OperationalError: near ")": syntax error
> }}}
>
> Walking through the code, I think that in
> `django/db/migrations/operations/fields.py` in
> `RemoveField.state_forwards`, maybe something like the following is
> needed
>
> {{{
> # check if we're deleting the last field
> if len(state.models[app_label, self.model_name_lower].fields) == 1:
> raise ValueError("Invalid operation to delete the last field")
> }}}
>
> Or maybe instead of generating a `RemoveField`, it should have been a
> `RemoveModel`

New description:

<strike>Or maybe instead of generating a `RemoveField`, it should have
been a `RemoveModel`</strike>

Edit: The migration file itself consisted of a `RemoveField` followed by a
`DeleteModel`, so maybe it would have sufficed to generate a `DeleteModel`
only

--

--
Ticket URL: <https://code.djangoproject.com/ticket/29344#comment:2>

Django

unread,
Apr 20, 2018, 8:46:53 AM4/20/18
to django-...@googlegroups.com
#29344: RemoveField on model with a single field
-------------------------------+--------------------------------------

Reporter: Shadi Akiki | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Shadi Akiki:

Old description:

> I came across this case accidentally, and it's probably not a common use-


> case, but I'm reporting it anyway.
> Also, it may as well be resolved in 2.0, but this was in 1.11.
>
> The following query got generated from one of my migration files
>
> {{{
> INSERT INTO "appname_modelname" () SELECT FROM "appname_modelname__old"
> }}}
>
> where I replaced my Application name with "appname" and model name with
> "modelname".
>
> This query threw an error in sqlite3.
>
> Here is the complete traceback
>
> {{{
> Traceback (most recent call last):
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/backends/utils.py", line 65, in execute


> return self.cursor.execute(sql, params)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/backends/sqlite3/base.py", line 330, in execute


> return Database.Cursor.execute(self, query, params)
> sqlite3.OperationalError: near ")": syntax error
>
> The above exception was the direct cause of the following exception:
>
> Traceback (most recent call last):
> File "./manage.py", line 22, in <module>
> execute_from_command_line(sys.argv)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/core/management/__init__.py", line 364, in
> execute_from_command_line
> utility.execute()
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/core/management/__init__.py", line 356, in execute
> self.fetch_command(subcommand).run_from_argv(self.argv)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/core/management/base.py", line 283, in


> run_from_argv
> self.execute(*args, **cmd_options)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/core/management/base.py", line 330, in execute


> output = self.handle(*args, **options)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/core/management/commands/migrate.py", line 204, in
> handle
> fake_initial=fake_initial,
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/migrations/executor.py", line 115, in migrate


> state = self._migrate_all_forwards(state, plan, full_plan, fake=fake,
> fake_initial=fake_initial)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/migrations/executor.py", line 145, in


> _migrate_all_forwards
> state = self.apply_migration(state, migration, fake=fake,
> fake_initial=fake_initial)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/migrations/executor.py", line 244, in


> apply_migration
> state = migration.apply(state, schema_editor)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/migrations/migration.py", line 129, in apply


> operation.database_forwards(self.app_label, schema_editor, old_state,
> project_state)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/migrations/operations/fields.py", line 155, in


> database_forwards
> schema_editor.remove_field(from_model,
> from_model._meta.get_field(self.name))
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/backends/sqlite3/schema.py", line 256, in


> remove_field
> self._remake_table(model, delete_field=field)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/db/backends/sqlite3/schema.py", line 206, in
> _remake_table
> self.quote_name(model._meta.db_table),
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/backends/base/schema.py", line 120, in execute
> cursor.execute(sql, params)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/db/backends/utils.py", line 80, in execute
> return super(CursorDebugWrapper, self).execute(sql, params)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/db/backends/utils.py", line 65, in execute
> return self.cursor.execute(sql, params)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/db/utils.py", line 94, in __exit__


> six.reraise(dj_exc_type, dj_exc_value, traceback)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/utils/six.py", line 685, in reraise
> raise value.with_traceback(tb)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5
> /site-packages/django/db/backends/utils.py", line 65, in execute


> return self.cursor.execute(sql, params)
> File "/home/shadi/.local/share/virtualenvs/BSEC_CMS2/lib/python3.5

> /site-packages/django/db/backends/sqlite3/base.py", line 330, in execute


> return Database.Cursor.execute(self, query, params)
> django.db.utils.OperationalError: near ")": syntax error
> }}}
>
> Walking through the code, I think that in
> `django/db/migrations/operations/fields.py` in
> `RemoveField.state_forwards`, maybe something like the following is
> needed
>
> {{{
> # check if we're deleting the last field
> if len(state.models[app_label, self.model_name_lower].fields) == 1:
> raise ValueError("Invalid operation to delete the last field")
> }}}
>

> <strike>Or maybe instead of generating a `RemoveField`, it should have
> been a `RemoveModel`</strike>
>
> Edit: The migration file itself consisted of a `RemoveField` followed by
> a `DeleteModel`, so maybe it would have sufficed to generate a
> `DeleteModel` only

New description:

~~Or maybe instead of generating a `RemoveField`, it should have been a
`RemoveModel`~~

Edit: The migration file itself consisted of a `RemoveField` followed by a
`DeleteModel`, so maybe it would have sufficed to generate a `DeleteModel`
only

--

--
Ticket URL: <https://code.djangoproject.com/ticket/29344#comment:3>

Django

unread,
Apr 20, 2018, 8:47:49 AM4/20/18
to django-...@googlegroups.com
#29344: RemoveField on model with a single field generates invalid SQL
-----------------------------+--------------------------------------

Reporter: Shadi Akiki | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.11
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => duplicate
* type: Uncategorized => Bug


Comment:

Duplicate of #24424.

--
Ticket URL: <https://code.djangoproject.com/ticket/29344#comment:4>

Django

unread,
Apr 20, 2018, 8:49:10 AM4/20/18
to django-...@googlegroups.com
#29344: RemoveField on model with a single field generates invalid SQL
-----------------------------+--------------------------------------

Reporter: Shadi Akiki | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.11
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------

Comment (by Shadi Akiki):

Oh ok thanks. Sorry I didn't find that issue before filing this one

--
Ticket URL: <https://code.djangoproject.com/ticket/29344#comment:5>

Reply all
Reply to author
Forward
0 new messages