django.db.utils.ProgrammingError: relation already exists

前端 未结 12 693
一向
一向 2020-12-23 14:11

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 Postgr

相关标签:
12条回答
  • 2020-12-23 15:04

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

    ./manage.py [--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 --database default --fake crud crud.0001_initial

    0 讨论(0)
  • 2020-12-23 15:05

    I found this problem in web2pyframework in models/config.py .

    Change

    settings.base.migrate = True

    on config file to

    settings.base.migrate = False

    Problem solved.

    0 讨论(0)
  • 2020-12-23 15:06

    I was facing the similar issues, where i had changed column name. I was getting same error as mentioned in the stack-trace provided with he question.

    Here's what I did.

    I ran fake migrations first. Then i removed it's(migrations i wanted to run) entry from django_migrations table and ran migrations again(no fake this time).

    Changes appeared as expected for me.

    hope this is helpful.

    0 讨论(0)
  • 2020-12-23 15:10

    I found and solved a particular example of this error in a Django 1.10 project while I was changing a foreign key field named member to point to a different table. I was changing this in three different models and I hit this error on all of them. In my first attempt I renamed member to member_user and tried to create a new field member as a foreign key pointing at the new table, but this didn't work.

    What I found is that when I renamed the member column it did not modify the index name in the form <app>_<model>_<hash> and when I tried to create a new member column it tried to create the same index name because the hash portion of the name was the same.

    I resolved the problem by creating a new member_user relation temporarily and copying the data. This created a new index with a different hash. I then deleted member and recreated it pointing at the new table and with it the would-be conflicting index name. Once that was done I ran the RunPython step to populate the new member column with references to the applicable table. I finished by adding RemoveField migrations to clean up the temporary member_user columns.

    I did have to split my migrations into two files because I received this error:

    psycopg2.OperationalError: cannot ALTER TABLE "<table_name>" because it has pending trigger events

    After the creation and copy of data into member_user I was not able to remove member in the same migration transaction. This may be a postgres specific limitation, but it was easily resolved by creating another transaction and moving everything after creating and copying member_user into the second migration.

    0 讨论(0)
  • 2020-12-23 15:14

    Do python manage.py migrate --fake initally.

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

    0 讨论(0)
  • 2020-12-23 15:15

    I recently had the same issue and tried some of the solutions here. manage.py migrate --fake led to a "django_content_type" does not exist error. Equally deleting old migrations might cause problems for other users if the migrations are shared.

    The manage.py squashmigrations command (docs) seems to be the ideal way to deal with this. Condenses old migrations into a single migration (which prevents applying them out of sequence etc), and preserves the old migrations for any other users. It worked in my case at least.

    0 讨论(0)
提交回复
热议问题