Jason Boyce
Jason Boyce

Reputation: 53

Django 3 Cannot migrate DB syntax error at or near "WITH ORDINALITY"

I recently upgraded my server from Python2.7 to Python3.7 and Django from 1.11 to 3.0.6. Most things are fine, but I am unable to migrate the database. Following this link: Errors when I try to migrate in Django2 I deleted all migrations, ran a new makemigrations, did a new migrate --fake and everything went fine.

Then I updated my models.py file, and it gave me the same error:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 233, in handle
    fake_initial=fake_initial,
  File "/webapps/jboycecom/local/lib/python3.7/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 "/webapps/jboycecom/local/lib/python3.7/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 "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 565, in alter_field
    old_db_params, new_db_params, strict)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/postgresql/schema.py", line 154, in _alter_field
    new_db_params, strict,
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 573, in _alter_field
    fk_names = self._constraint_names(model, [old_field.column], foreign_key=True)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 1148, in _constraint_names
    constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/postgresql/introspection.py", line 166, in get_constraints
    """, [table_name])
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/webapps/jboycecom/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: syntax error at or near "WITH ORDINALITY"
LINE 6:                     FROM unnest(c.conkey) WITH ORDINALITY co...

I feel that I may have bunged up the database when updating Django, as it complained about a non-working oauth2 import that I wasn't using anymore, but since I couldn't migrate the database, I just deleted the offending model, and everything else compiled and has been working fine.

I am not sure what else to try. One part of the earlier link said "Truncate the _django_migrations_ table" but I don't know how to do that. Google hasn't given me anything helpful. Maybe that will help?

Upvotes: 1

Views: 785

Answers (1)

Jason Boyce
Jason Boyce

Reputation: 53

Well, I couldn't figure it out, so I just deleted the whole database and will repopulate via a spreadsheet I exported in advance.


ACTUALLY, this did not fix the issue. Attempting to migrate changes to ForeignKey or ManyToManyFields in Django will give me the error if I try to migrate the database. I created a new CharField and it migrated just fine.

I am running PSQL 9.5.21 and Psycopg 2.8.5 so those aren't the problem.

Upvotes: 1

Related Questions