{{{
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/22720>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
This is migration file that gets created by makemigrations:
{{{
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.CreateModel(
name='Book',
fields=[
('id', models.AutoField(verbose_name='ID',
serialize=False, auto_created=True, primary_key=True)),
('title', models.CharField(max_length=100,
db_index=True)),
],
options={
},
bases=(models.Model,),
),
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(verbose_name='ID',
serialize=False, auto_created=True, primary_key=True)),
('comment', models.TextField()),
('book', models.ForeignKey(to_field='id',
to='testapp.Book')),
('_order', models.OrderWrt()),
],
options={
'order_with_respect_to': 'book',
},
bases=(models.Model,),
),
]
}}}
So might be something with the migration picking up on the '_order' field
and including it itself, and therefore it gets inserted twice in the SQL?
--
Ticket URL: <https://code.djangoproject.com/ticket/22720#comment:1>
* stage: Unreviewed => Accepted
Comment:
Managed to reproduce on SQLite3. Might be related to #22720.
--
Ticket URL: <https://code.djangoproject.com/ticket/22720#comment:2>
Comment (by valberg):
Removing
{{{
('_order', models.OrderWrt()),
}}}
makes the migrate command succeed, and there is a _order column on the
database table.
It might be a simple "do not include OrderWrt in the makemigrations
command".
--
Ticket URL: <https://code.djangoproject.com/ticket/22720#comment:3>
* owner: nobody => valberg
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/22720#comment:4>
Comment (by valberg):
Removing:
{{{
options={
'order_with_respect_to': 'book',
},
}}}
from the migration (but keeping `('_order', models.OrderWrt()),`) also
makes the migration work just fine.
--
Ticket URL: <https://code.djangoproject.com/ticket/22720#comment:5>
Comment (by andrewgodwin):
The correct approach here is to remove the _order field from the generated
migration's list of fields. I'm currently rewriting the autodetector so I
can do this if you want, valberg.
--
Ticket URL: <https://code.djangoproject.com/ticket/22720#comment:6>
* has_patch: 0 => 1
Comment:
I've made the following commit after discussion with andrewgodwin on irc:
https://github.com/django/django/pull/2734
--
Ticket URL: <https://code.djangoproject.com/ticket/22720#comment:7>
Comment (by Andrew Godwin <andrew@…>):
In [changeset:"61185bba72dbb13e4f5f233f6be790957a57914c"]:
{{{
#!CommitTicketReference repository=""
revision="61185bba72dbb13e4f5f233f6be790957a57914c"
Merge pull request #2734 from valberg/double_order_trouble
Fixed #22720 -- Migrations attempt to create _order twice.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22720#comment:9>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"6cfa2fae3963c11e4c8ad180decba2928736dba0"]:
{{{
#!CommitTicketReference repository=""
revision="6cfa2fae3963c11e4c8ad180decba2928736dba0"
Fixed #22720 -- Migrations attempt to create _order twice.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22720#comment:8>
Comment (by Tim Graham <timograham@…>):
In [changeset:"0ee27d5b62a76781f5d78fc3f506dca624941062"]:
{{{
#!CommitTicketReference repository=""
revision="0ee27d5b62a76781f5d78fc3f506dca624941062"
[1.7.x] Fixed #22720 -- Migrations attempt to create _order twice.
Backport of 6cfa2fae39 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22720#comment:10>