Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#22720 closed Bug (fixed)

Migrating a model with 'order_with_respect_to' tries to create the '_order' colum twice.

Reported by: Vidir Valberg Gudmundsson Owned by: Vidir Valberg Gudmundsson
Component: Migrations Version: dev
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (10)

comment:1 by Vidir Valberg Gudmundsson, 10 years ago

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?

comment:2 by Simon Charette, 10 years ago

Triage Stage: UnreviewedAccepted

Managed to reproduce on SQLite3. Might be related to #22720.

Version 0, edited 10 years ago by Simon Charette (next)

comment:3 by Vidir Valberg Gudmundsson, 10 years ago

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".

comment:4 by Vidir Valberg Gudmundsson, 10 years ago

Owner: changed from nobody to Vidir Valberg Gudmundsson
Status: newassigned

comment:5 by Vidir Valberg Gudmundsson, 10 years ago

Removing:

            options={
                'order_with_respect_to': 'book',
            },

from the migration (but keeping ('_order', models.OrderWrt()),) also makes the migration work just fine.

comment:6 by Andrew Godwin, 10 years ago

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.

comment:7 by Vidir Valberg Gudmundsson, 10 years ago

Has patch: set

I've made the following commit after discussion with andrewgodwin on irc:

https://github.com/django/django/pull/2734

comment:8 by Víðir Valberg Guðmundsson <valberg@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 6cfa2fae3963c11e4c8ad180decba2928736dba0:

Fixed #22720 -- Migrations attempt to create _order twice.

comment:9 by Andrew Godwin <andrew@…>, 10 years ago

In 61185bba72dbb13e4f5f233f6be790957a57914c:

Merge pull request #2734 from valberg/double_order_trouble

Fixed #22720 -- Migrations attempt to create _order twice.

comment:10 by Tim Graham <timograham@…>, 10 years ago

In 0ee27d5b62a76781f5d78fc3f506dca624941062:

[1.7.x] Fixed #22720 -- Migrations attempt to create _order twice.

Backport of 6cfa2fae39 from master

Note: See TracTickets for help on using tickets.
Back to Top