Dan Swain
Dan Swain

Reputation: 3108

'syntax error at or near "ORDER" SQL' when running Django Migration

Environment: Ubuntu/Apache

I am getting the following error when running a migration. Below the error is the migration file. The migration runs fine on a dev machine, but is erroring out when trying to deploy. The error is coming up within the Django code, so I don't have a lot to go on in order to sort this out. A summary of the migration is that it is removing null=True from three ManyToManyField fields and is changing the upload_to value for the remaining fields shown. Django (1.11.9) and psycopg2 (2.7.3.2) versions match on both machines. Is it possible to tell from this error message which field (if a specific field is the problem) is causing the error?

Running migrations:
  Applying manager.0035_auto_20180119_1138...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/usr/local/lib/python2.7/dist-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 "/usr/local/lib/python2.7/dist-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 "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 221, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 500, in alter_field
    return self._alter_many_to_many(model, old_field, new_field, strict)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 845, in _alter_many_to_many
    new_field.remote_field.through._meta.get_field(new_field.m2m_reverse_field_name()),
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 515, in alter_field
    old_db_params, new_db_params, strict)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql/schema.py", line 112, in _alter_field
    new_db_params, strict,
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 524, in _alter_field
    fk_names = self._constraint_names(model, [old_field.column], foreign_key=True)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 995, in _constraint_names
    constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql/introspection.py", line 233, in get_constraints
    """, [table_name])
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: syntax error at or near "ORDER"
LINE 3:                 indexname, array_agg(attname ORDER BY rnum),...

Here is the migration file that is failing:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2018-01-19 12:10
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('manager', '0034_auto_20170126_1808'),
    ]

    operations = [
        migrations.AlterField(
            model_name='book',
            name='topics',
            field=models.ManyToManyField(blank=True, to='manager.Topic'),
        ),
        migrations.AlterField(
            model_name='catalog',
            name='books',
            field=models.ManyToManyField(blank=True, to='manager.Book'),
        ),
        migrations.AlterField(
            model_name='catalog',
            name='thumb',
            field=models.ImageField(blank=True, null=True, upload_to=b'images/catalogs/'),
        ),
        migrations.AlterField(
            model_name='customer',
            name='email_lists',
            field=models.ManyToManyField(blank=True, to='manager.EmailList'),
        ),
        migrations.AlterField(
            model_name='graphic',
            name='image',
            field=models.ImageField(blank=True, null=True, upload_to=b'images'),
        ),
        migrations.AlterField(
            model_name='graphic',
            name='thumb',
            field=models.ImageField(blank=True, null=True, upload_to=b'images/thumb/'),
        ),
        migrations.AlterField(
            model_name='navigationitem',
            name='image',
            field=models.ImageField(blank=True, null=True, upload_to=b'images/nav/'),
        ),
        migrations.AlterField(
            model_name='navigationitem',
            name='rollover',
            field=models.ImageField(blank=True, null=True, upload_to=b'images/nav/'),
        ),
        migrations.AlterField(
            model_name='photo',
            name='image',
            field=models.ImageField(blank=True, null=True, upload_to=b'images'),
        ),
        migrations.AlterField(
            model_name='photo',
            name='thumb',
            field=models.ImageField(blank=True, null=True, upload_to=b'images/thumb/'),
        ),
        migrations.AlterField(
            model_name='template',
            name='html',
            field=models.FileField(blank=True, null=True, upload_to=b'page_templates/'),
        ),
    ]

Upvotes: 0

Views: 714

Answers (1)

Alasdair
Alasdair

Reputation: 309029

Your error is similar to the one in ticket 28736, so it looks as if you are running PostgreSQL 8.X.

Django 1.11 requires at least PostgreSQL 9.3.

Upvotes: 1

Related Questions