django.db.utils.ProgrammingError: relation already exists

112,409

Solution 1

This works pretty fine

./manage.py migrate --fake default

https://docs.djangoproject.com/en/2.2/ref/django-admin/#cmdoption-migrate-fake

Solution 2

Do python manage.py migrate --fake initally.

https://docs.djangoproject.com/en/3.2/ref/django-admin/#django-admin-migrate

Solution 3

I've faced similar issue when added couple new fields to existing model. I'm using Django 1.9, which introduced --run-syncdb option. Running manage.py migrate --run-syncdb fixed my tables.

Solution 4

Now (I'm using Django 1.9) you can make:

./manage.py migrate [--database DATABASE] --fake [app_label] [migration_name]

This way you're targeting the problem with more accuracy, and you can fake only the problematic migration on the specific database.

So, looking at the question, you could:

./manage.py migrate --database default --fake crud crud.0001_initial

Solution 5

Been facing a similar issue, eventually deleted all .py files in migration folder (django 1.7 creates one automatically), worked perfectly after that.

Share:
112,409
quindraco
Author by

quindraco

Updated on February 01, 2022

Comments

  • quindraco
    quindraco over 2 years

    I'm trying to set up the tables for a new django project (that is, the tables do NOT already exist in the database); the django version is 1.7 and the db back end is PostgreSQL. The name of the project is crud. Results of migration attempt follow:

    python manage.py makemigrations crud

    Migrations for 'crud':
      0001_initial.py:
        - Create model AddressPoint
        - Create model CrudPermission
        - Create model CrudUser
        - Create model LDAPGroup
        - Create model LogEntry
        - Add field ldap_groups to cruduser
        - Alter unique_together for crudpermission (1 constraint(s))
    

    python manage.py migrate crud

    Operations to perform:
      Apply all migrations: crud
    Running migrations:
      Applying crud.0001_initial...Traceback (most recent call last):
      File "manage.py", line 18, in <module>
        execute_from_command_line(sys.argv)
      File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
        utility.execute()
      File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 377, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 288, in run_from_argv
        self.execute(*args, **options.__dict__)
      File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 338, in execute
        output = self.handle(*args, **options)
      File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 161, in handle
        executor.migrate(targets, plan, fake=options.get("fake", False))
      File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 68, in migrate
        self.apply_migration(migration, fake=fake)
      File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 102, in apply_migration
        migration.apply(project_state, schema_editor)
      File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 108, in apply
        operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
      File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/models.py", line 36, in database_forwards
        schema_editor.create_model(model)
      File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 262, in create_model
        self.execute(sql, params)
      File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 103, in execute
        cursor.execute(sql, params)
      File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 82, in execute
        return super(CursorDebugWrapper, self).execute(sql, params)
      File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 66, in execute
        return self.cursor.execute(sql, params)
      File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
        six.reraise(dj_exc_type, dj_exc_value, traceback)
      File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 66, in execute
        return self.cursor.execute(sql, params)
    django.db.utils.ProgrammingError: relation "crud_crudpermission" already exists
    

    Some highlights from the migration file:

    dependencies = [
        ('auth', '0001_initial'),
        ('contenttypes', '0001_initial'),
    ]
        migrations.CreateModel(
            name='CrudPermission',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('_created_by', models.CharField(default=b'', max_length=64, null=True, editable=False, blank=True)),
                ('_last_updated_by', models.CharField(default=b'', max_length=64, null=True, editable=False, blank=True)),
                ('_created', models.DateTimeField(null=True, editable=False, blank=True)),
                ('_last_updated', models.DateTimeField(null=True, editable=False, blank=True)),
                ('domain', models.CharField(max_length=32, choices=[(b'town', b'Town'), (b'boe', b'BOE'), (b'police', b'Police')])),
                ('ldap_group', models.CharField(max_length=128, verbose_name=b'LDAP group')),
                ('can_add', models.BooleanField(default=False, verbose_name=b'add')),
                ('can_change', models.BooleanField(default=False, verbose_name=b'change')),
                ('restrict_change_to_own', models.BooleanField(default=False)),
                ('can_delete', models.BooleanField(default=False, verbose_name=b'delete')),
                ('restrict_delete_to_own', models.BooleanField(default=False)),
                ('models', models.ManyToManyField(to='contenttypes.ContentType', null=True, blank=True)),
            ],
            options={
                'verbose_name': 'CRUD permission',
            },
            bases=(models.Model,),
        ),
        migrations.AlterUniqueTogether(
            name='crudpermission',
            unique_together=set([('ldap_group', 'can_add', 'can_change', 'can_delete', 'domain')]),
        )
    

    ,

    The crud app is not meant to actually do anything, but I use it another app, so when I try migrate from that app, I trigger the above problem.

    I've found other examples on the web of people with similar issues, but none of their cases seem to apply because

    1. The problem affects an entire relation, not just one column
    2. I am not using multiple inheritance.

    Where should I look next to find the underlying problem?