M
M
Maxim2017-08-02 15:16:21
Django
Maxim, 2017-08-02 15:16:21

How to apply migration with Duplicate column name error?

Changed model from

class Communities(models.Model):
    id = models.CharField(primary_key=True, blank=True, max_length=255)
    name = models.CharField(blank=True, max_length=500)
    description = models.CharField(blank=True, max_length=500)
    link = models.CharField(blank=True, max_length=500)
    photo_link = models.CharField(blank=True, max_length=500)
    members = models.CharField(blank=True, max_length=500)
    total_score = models.BigIntegerField(default=0)

on the
class Communities(models.Model):
    communitie_id = models.CharField(blank=True, max_length=255)
    owner = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
    social_network = models.CharField(blank=True, max_length=255)
    name = models.CharField(blank=True, max_length=255)
    description = models.TextField(blank=True, max_length=500)
    link = models.CharField(blank=True, max_length=255)
    photo_link = models.TextField(blank=True, max_length=500)
    members = models.BigIntegerField(default=0)
    total_score = models.BigIntegerField(default=0)

Ran makemigrations
# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2017-08-02 12:08
from __future__ import unicode_literals

import datetime
from django.db import migrations, models
import django.db.models.deletion
from django.utils.timezone import utc


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0013_auto_20170801_1448'),
    ]

    operations = [
        migrations.AddField(
            model_name='communities',
            name='communitie_id',
            field=models.CharField(blank=True, max_length=255),
        ),
        migrations.AddField(
            model_name='communities',
            name='owner',
            field=models.ForeignKey(default=2, on_delete=django.db.models.deletion.CASCADE, to='api.UserProfile'),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='communities',
            name='social_network',
            field=models.CharField(blank=True, max_length=255),
        ),
        migrations.AlterField(
            model_name='communities',
            name='description',
            field=models.TextField(blank=True, max_length=500),
        ),
        migrations.AlterField(
            model_name='communities',
            name='id',
            field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
        ),
        migrations.AlterField(
            model_name='communities',
            name='link',
            field=models.CharField(blank=True, max_length=255),
        ),
        migrations.AlterField(
            model_name='communities',
            name='members',
            field=models.BigIntegerField(default=0),
        ),
        migrations.AlterField(
            model_name='communities',
            name='name',
            field=models.CharField(blank=True, max_length=255),
        ),
        migrations.AlterField(
            model_name='communities',
            name='photo_link',
            field=models.TextField(blank=True, max_length=500),
        ),
        migrations.AlterField(
            model_name='message',
            name='time_send',
            field=models.DateTimeField(default=datetime.datetime(2017, 8, 2, 12, 8, 24, 147790, tzinfo=utc)),
        ),
    ]

When I migrate I get an error
Running migrations:
  Rendering model states... DONE
  Applying api.0014_auto_20170802_1208...Traceback (most recent call last):
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 112, in execute
    return self.cursor.execute(query, args)
  File "/home/maxim/buzz/lib/python3.5/site-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/home/maxim/buzz/lib/python3.5/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
  File "/home/maxim/buzz/lib/python3.5/site-packages/MySQLdb/cursors.py", line 217, in execute
    res = self._query(query)
  File "/home/maxim/buzz/lib/python3.5/site-packages/MySQLdb/cursors.py", line 378, in _query
    rowcount = self._do_query(q)
  File "/home/maxim/buzz/lib/python3.5/site-packages/MySQLdb/cursors.py", line 341, in _do_query
    db.query(q)
  File "/home/maxim/buzz/lib/python3.5/site-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1060, "Duplicate column name 'communitie_id'")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
    field,
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/backends/mysql/schema.py", line 50, in add_field
    super(DatabaseSchemaEditor, self).add_field(model, field)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 396, in add_field
    self.execute(sql, params)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/maxim/buzz/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 112, in execute
    return self.cursor.execute(query, args)
  File "/home/maxim/buzz/lib/python3.5/site-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/home/maxim/buzz/lib/python3.5/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
  File "/home/maxim/buzz/lib/python3.5/site-packages/MySQLdb/cursors.py", line 217, in execute
    res = self._query(query)
  File "/home/maxim/buzz/lib/python3.5/site-packages/MySQLdb/cursors.py", line 378, in _query
    rowcount = self._do_query(q)
  File "/home/maxim/buzz/lib/python3.5/site-packages/MySQLdb/cursors.py", line 341, in _do_query
    db.query(q)
  File "/home/maxim/buzz/lib/python3.5/site-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1060, "Duplicate column name 'communitie_id'")

Tried to run migrate --fake-initial without success, what is the correct way to migrate?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question