#30743: Removing `db_index=True` crashes migrations
-------------------------------------+-------------------------------------
Reporter: John Tsai | Owner: (none)
Type: Bug | Status: new
Component: | Version: 2.2
contrib.postgres |
Severity: Normal | Keywords: postgres, db_index
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
When I switch from a development branch to the testing branch, the
`db_index=True` was removed. But migration failed and raised such error:
{{{
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/core/management/__init__.py", line 381, in
execute_from_command_line
utility.execute()
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/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 "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/migrations/executor.py", line 147, in
_migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake,
fake_initial=fake_initial)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state,
project_state)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/migrations/operations/fields.py", line 249, in
database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/backends/base/schema.py", line 535, in alter_field
old_db_params, new_db_params, strict)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/backends/postgresql/schema.py", line 124, in
_alter_field
new_db_params, strict,
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/backends/base/schema.py", line 603, in _alter_field
exclude=meta_index_names,
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/backends/base/schema.py", line 1115, in
_constraint_names
constraints = self.connection.introspection.get_constraints(cursor,
model._meta.db_table)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/backends/postgresql/introspection.py", line 175, in
get_constraints
""", ["public", table_name])
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False,
executor=self._execute)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/blesssystem/venv/lib/python3.6/site-
packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.SyntaxError: syntax error at or near "WITH ORDINALITY"
LINE 6: FROM unnest(c.conkey) WITH ORDINALITY co...
}}}
I tried to print out the executing migrate query
{{{
#!sql
SELECT
c.conname,
array(
SELECT attname
FROM unnest(c.conkey) WITH ORDINALITY cols(colid,
arridx)
JOIN pg_attribute AS ca ON cols.colid = ca.attnum
WHERE ca.attrelid = c.conrelid
ORDER BY cols.arridx
),
c.contype,
(SELECT fkc.relname || '.' || fka.attname
FROM pg_attribute AS fka
JOIN pg_class AS fkc ON fka.attrelid = fkc.oid
WHERE fka.attrelid = c.confrelid AND fka.attnum =
c.confkey[1]),
cl.reloptions
FROM pg_constraint AS c
JOIN pg_class AS cl ON c.conrelid = cl.oid
JOIN pg_namespace AS ns ON cl.relnamespace = ns.oid
WHERE ns.nspname = %s AND cl.relname = %s
}}}
I also tried another way to create the index, and it seems working well
after removing it.
{{{
#!python
class MyModel(models.Model):
my_field = models.CharField(max_length=64, db_index=True) # will
crash after removed
class Meta: # fine even if removed
indexes = [
models.Index(fields=['my_field'])
]
}}}
I could not find any solution to solve it in Python code (Remove by
`dbshell` and use fake migrate might work though). But I believe the
**Meta** version works, then **db_index** might work too.
----
Environment
django version 2.2.3
postgreSQL 9.5.13
psycopg2-binary 2.8.2
--
Ticket URL: <https://code.djangoproject.com/ticket/30743>
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/049.a511d99f7c4b867cd916aa5449b9453f%40djangoproject.com.