#30743 closed Bug (worksforme)
Removing `db_index=True` crashes migrations.
Reported by: | John Tsai | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | postgres, db_index |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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
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.
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
Change History (2)
comment:1 by , 5 years ago
Component: | contrib.postgres → Database layer (models, ORM) |
---|---|
Owner: | set to |
Resolution: | → worksforme |
Status: | new → closed |
Summary: | Removing `db_index=True` crashes migrations → Removing `db_index=True` crashes migrations. |
Version: | 2.2 → master |
comment:2 by , 5 years ago
psql (9.5.13, server 9.3.23)
Seems that my psql version is not the same as the server version! Thanks for trying out and replying.
Crashing SQL is related with introspection on PostgreSQL. I couldn't reproduce this issue.
WITH ORDINALITY
is supported in PostgreSQL 9.4+, maybe you used unsupported version of db.