﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22720	Migrating a model with 'order_with_respect_to' tries to create the '_order' colum twice.	Vidir Valberg Gudmundsson	Vidir Valberg Gudmundsson	"Having the following models:

{{{
class Book(models.Model):
    title = models.CharField(max_length=100, db_index=True)


class Comment(models.Model):
    comment = models.TextField()
    book = models.ForeignKey('testapp.Book')

    class Meta:
        order_with_respect_to = 'book'
}}}


Creating migrations works fine:

{{{
$ ./manage.py makemigrations testapp
Migrations for 'testapp':
0001_initial.py:
    - Create model Book
    - Create model Comment

}}}

Running them is another matter:

{{{
$ ./manage.py migrate
Operations to perform:
Synchronize unmigrated apps: contenttypes, auth, sessions, admin
Apply all migrations: testapp
Synchronizing apps without migrations:
Creating tables...
Installing custom SQL...
Installing indexes...
Running migrations:
Applying testapp.0001_initial...Traceback (most recent call last):
File ""/home/valberg/Code/projects/django/django/db/backends/utils.py"", line 63, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column ""_order"" specified more than once


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File ""./manage.py"", line 10, in <module>
    execute_from_command_line(sys.argv)
File ""/home/valberg/Code/projects/django/django/core/management/__init__.py"", line 384, in execute_from_command_line
    utility.execute()
File ""/home/valberg/Code/projects/django/django/core/management/__init__.py"", line 376, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
File ""/home/valberg/Code/projects/django/django/core/management/base.py"", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
File ""/home/valberg/Code/projects/django/django/core/management/base.py"", line 337, in execute
    output = self.handle(*args, **options)
File ""/home/valberg/Code/projects/django/django/core/management/commands/migrate.py"", line 146, in handle
    executor.migrate(targets, plan, fake=options.get(""fake"", False))
File ""/home/valberg/Code/projects/django/django/db/migrations/executor.py"", line 62, in migrate
    self.apply_migration(migration, fake=fake)
File ""/home/valberg/Code/projects/django/django/db/migrations/executor.py"", line 96, in apply_migration
    migration.apply(project_state, schema_editor)
File ""/home/valberg/Code/projects/django/django/db/migrations/migration.py"", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File ""/home/valberg/Code/projects/django/django/db/migrations/operations/models.py"", line 30, in database_forwards
    schema_editor.create_model(model)
File ""/home/valberg/Code/projects/django/django/db/backends/schema.py"", line 267, in create_model
    self.execute(sql, params)
File ""/home/valberg/Code/projects/django/django/db/backends/schema.py"", line 98, in execute
    cursor.execute(sql, params)
File ""/home/valberg/Code/projects/django/django/db/backends/utils.py"", line 78, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
File ""/home/valberg/Code/projects/django/django/db/backends/utils.py"", line 63, in execute
    return self.cursor.execute(sql, params)
File ""/home/valberg/Code/projects/django/django/db/utils.py"", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
File ""/home/valberg/Code/projects/django/django/utils/six.py"", line 549, in reraise
    raise value.with_traceback(tb)
File ""/home/valberg/Code/projects/django/django/db/backends/utils.py"", line 63, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column ""_order"" specified more than once
}}}


Apparently the column '_order' gets inserted twice in the SQL statement."	Bug	closed	Migrations	dev	Release blocker	fixed			Accepted	1	0	0	0	0	0
