1

I have added some verbose names onto a series of FK fields in my schema [all the way thru a chain of related tables] but get this error when I try to run

python manage.py migrate

or python manage.py migrate --fake

django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey" DETAIL: Key (id)=(10) already exists.

there is data in my db already. all running on a local machine in Windows 7, using postgres on local host.

full trace is below

C:\Users\aakh\Documents\Gits\testcap>python manage.py migrate
System check identified some issues:

WARNINGS:
?: (1_6.W001) Some project unittests may not execute as expected.
        HINT: Django 1.6 introduced a new default test runner. It looks like this project was generated using Django 1.5 or earlier. You should ensure your tests are all running & behaving as expected. See https://do
cs.djangoproject.com/en/dev/releases/1.6/#new-test-runner for more information.
Operations to perform:
  Apply all migrations: tccore, sessions, admin, sites, auth, contenttypes
Running migrations:
  Applying tccore.0006_auto_20150130_1243...Traceback (most recent call last):
  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 385, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 108, in apply_migration
    self.recorder.record_applied(migration.app_label, migration.name)
  File "C:\Python27\lib\site-packages\django\db\migrations\recorder.py", line 67, in record_applied
    self.migration_qs.create(app=app, name=name)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 372, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 589, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 617, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 698, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 731, in _do_insert
    using=using, raw=raw)
  File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 921, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 920, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 65, 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 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"
DETAIL:  Key (id)=(10) already exists.
Ammar Akhtar
  • 1,698
  • 3
  • 13
  • 25
  • are you migrating everything at a time? It seems there is an issue with the tccore.0006_auto_20150130_1243.. migration - could you paste that? It seems that you're somehow trying to run a migration that has already been run or something.. mmh – Martin B. Jan 30 '15 at 13:23

1 Answers1

-2

figured it out ... I was adding verbose name to ForeignKey, when it should have been added on the relevant field in the related object

Correct:

class AppReference(BaseModel):
    name = models.CharField(max_length=200, unique=True, verbose_name="App name")

class App(BaseModel):

    app_reference = models.ForeignKey(AppReference)
    version = models.CharField(max_length=10)

Incorrect:

class AppReference(BaseModel):
    name = models.CharField(max_length=200, unique=True)

class App(BaseModel):

    app_reference = models.ForeignKey(AppReference, verbose_name="App name")
    version = models.CharField(max_length=10)
Ammar Akhtar
  • 1,698
  • 3
  • 13
  • 25