django.db.utils.ProgrammingError: relation already exists

前端 未结 12 831
一向
一向 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条回答
  •  猫巷女王i
    2020-12-23 14:53

    I've been dealing with this for several years. There could be different scenarios:

    Scenario 1: as in the original post, you had no tables to start with. In this case, I'd

    1. comment out the relationship in models.py
    2. run python manage.py
    3. migrate assuming that it now succeeds
    4. uncomment what you
    5. commented out in step 1 run python manage.py migrate --fake

    Scenario 2: multiple apps: One possibility is that you might have different apps and the data model of one app is using some tables from the other app. In this case, if the data model is designed properly, you should be able to create the tables for one app only (by specifying only that one in setting.py), then add the other app and migrate. If it is not design with care, and there are recursive dependencies, I suggest changing the design rather than making a temporary fix.

    Scenario 3: you had some tables and something went wrong with your migration, then I'd

    1. revert models.py to what it was and only introduce the new relationship that appears to already exist in models.py.
    2. delete migration folder
    3. run python manage.py makemigrations
    4. introduce new changes to models.py if any and continue with makemigrations and migrate commands as usual.

提交回复
热议问题