3

This question had been asked before here, but for a different scenario.

I am working on a Django/Wagtail project. At some point I had to modify a model, and add some fields.

Accidentally I included a flag unique=True for a new field. This way:

title = models.CharField(max_length=100, unique=True, blank=True, null=True, verbose_name=_('Category Title'))

When making migrations and migrating, it yelled this issue:

enter image description here

Then I realized the mistake, removed the unique=True and left it like this:

title = models.CharField(max_length=100, blank=True, null=True, verbose_name=_('Category Title'))

I made the migrations and migrated again, expecting the issue to go away. However I get the same issue.

How can I solve this?

This is the last migrations:

class Migration(migrations.Migration):

    dependencies = [
        ('distributed', '0029_remove_blogcategory_title'),
    ]

    operations = [
        migrations.AddField(
            model_name='blogcategory',
            name='title',
            field=models.CharField(blank=True, max_length=100, null=True, verbose_name='Category Title'),
        ),
    ]

And this is the trace:

    Microsoft Windows [Versión 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. Reservados todos los derechos.

C:\Windows\system32>cd C:/

C:\>cd distributed

C:\distributed>cd distributed

C:\distributed\distributed>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 6 unapplied migration(s). Your project may not work properly until you
apply the migrations for app(s): distributed.
Run 'python manage.py migrate' to apply them.
April 26, 2017 - 12:40:45
Django version 1.10.7, using settings 'genesis.settings.dev'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
^C
C:\distributed\distributed>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, distributed, otp_static, otp_
totp, otp_yubikey, sessions, sites, taggit, two_factor, wagtailadmin, wagtailcor
e, wagtaildocs, wagtailembeds, wagtailforms, wagtailimages, wagtailredirects, wa
gtailsearch, wagtailusers
Running migrations:
  Applying distributed.0025_auto_20170425_1827...Traceback (most recent call las
t):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
367, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 294,
 in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 345,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py
", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 11
5, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_i
nitial=fake_initial)
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 14
5, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_
initial)
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 24
4, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 1
29, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
  File "C:\Python27\lib\site-packages\django\db\migrations\operations\fields.py"
, line 84, in database_forwards
    field,
  File "C:\Python27\lib\site-packages\django\db\backends\postgresql\schema.py",
line 21, in add_field
    super(DatabaseSchemaEditor, self).add_field(model, field)
  File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 4
09, in add_field
    self.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 1
12, in execute
    cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 79, in
execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python27\lib\site-packages\cachalot\monkey_patch.py", line 111, in in
ner
    out = original(cursor, sql, *args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in
execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in
execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: could not create unique index "distributed_blogc
ategory_title_key"
DETAIL:  Key (title)=() is duplicated.


C:\distributed\distributed>
Community
  • 1
  • 1
Pablo Viacheslav
  • 873
  • 1
  • 8
  • 15
  • Just added them – Pablo Viacheslav Apr 26 '17 at 16:29
  • The traceback is telling you that the error is occurring in `distributed.0025_auto_20170425_1827` - I don't think that's the migration you have shown. What is the output of `python manage.py showmigrations distributed`? – Alasdair Apr 26 '17 at 16:34
  • You are right. This is where my 'newbieness' is evident. I thought new migrations "overwrote" old ones. So, should I delete migrations up to the one causing the error and re run the make migrations? – Pablo Viacheslav Apr 26 '17 at 16:38
  • **Wait before deleting any migrations**. If you delete migrations that have already been applied, it can be extremely difficult to get your models and database back into sync. Run `python manage.py showmigrations distributed` to see what migrations have been applied. – Alasdair Apr 26 '17 at 16:41
  • I never ran this before, however I can figure out, I think. This is the result: ` [X] 0022_auto_20170109_1620` ` [X] 0023_auto_20170109_1621` ` [X] 0024_auto_20170112_1933` ` [ ] 0025_auto_20170425_1827` ` [ ] 0026_auto_20170425_1829` ` [ ] 0027_auto_20170425_1839` ` [ ] 0028_auto_20170425_1840` ` [ ] 0029_remove_blogcategory_title ` ` [ ] 0030_blogcategory_title` – Pablo Viacheslav Apr 26 '17 at 16:44
  • I assume, it shows number 24 was the last applied migration, so, it is save to delete up to 25, correct? – Pablo Viacheslav Apr 26 '17 at 16:46
  • 1
    Yes, that's correct. If you delete 25-30 (or back them up to be safe), then run `makemigrations` and `migrate` then it should work. – Alasdair Apr 26 '17 at 16:55
  • It woked! Not sure if I can mark your comment as the correct answer directly, if not, and want to make it an official answer I will mark is as the correct one. – Pablo Viacheslav Apr 26 '17 at 18:17
  • Glad it worked :) – Alasdair Apr 26 '17 at 18:21

1 Answers1

6

The traceback is telling you that the error occurs when you try to run migration 0025.

The output of python manage.py showmigrations tells you that migrations 0025-0030 have not been applied.

[X] 0022_auto_20170109_1620
[X] 0023_auto_20170109_1621
[X] 0024_auto_20170112_1933
[ ] 0025_auto_20170425_1827
[ ] 0026_auto_20170425_1829
[ ] 0027_auto_20170425_1839
[ ] 0028_auto_20170425_1840
[ ] 0029_remove_blogcategory_title
[ ] 0030_blogcategory_title

Therefore it should be safe to delete these migrations (you might want to back them up to be on the safe side). You can then rerun makemigrations and migrate to re-create and run new migrations.

Alasdair
  • 298,606
  • 55
  • 578
  • 516