django.db.utils.ProgrammingError: relation already exists

前端 未结 12 828
一向
一向 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: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 __ 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 "" 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.

提交回复
热议问题