Django migrations : relation already exists

前端 未结 6 879
南笙
南笙 2021-02-06 18:46

I have trouble with django model migrations. I have some models in my app, and I already have some data inside. When I added some models in my application, and I run mak

相关标签:
6条回答
  • 2021-02-06 19:15

    The main problem is the existing tables that are disabling the migration of the new tables, so the solution is straight-forward:

    ** Try to add managed = False to the existing dB so it won't be detected by migrate

    ** Redo it for all existing old tables :

    class Meta:
        managed=False
    

    It sometimes gets boring when we have a lot of tables in the same application but it works perfectly!

    0 讨论(0)
  • 2021-02-06 19:16

    every time you make changes to your models, try these steps :

    python manage.py makemigrations [your app name]
    

    then:

    python manage.py migrate
    

    it should work fine. but remember if you have already data(rows) in your tables you should specify the default value for each one the queries.
    if not, Django prompt you to specify the default value for them or you can just try to use blank=True or null=True in your fields like below :

    website         = models.URLField(blank=True)
    
    0 讨论(0)
  • 2021-02-06 19:17

    the possible cause or this is that you have another migration in the same folder starts with the same prefix... maybe you make another migration on the same table on another branch or commit so it's saved to the db with the same prefix ie: 00010_migration_from_commit_#10, 00010_migration_from_commit_#11

    the solution for this is to rename the migration file like this 00011_migration_from_commit_#11

    0 讨论(0)
  • 2021-02-06 19:21

    check your db tables to make sure you are not trying to recreate the table during migrations. that's what it was for me. If you have data in the table that you don't want to delete, remove all migrations from the app_name/migrations folder and then run ./manage.py migrate app_name --fake default

    then try makemigrations and migrate for the rest

    0 讨论(0)
  • 2021-02-06 19:22

    How about doing this way ?

    python manage.py makemigrations

    (Skip this step if you have already have migration file ready)

    It will create migrations for that package lets say with a name like 0001_initial.py

    Edit the file manually so that you delete all models there except that was already created in database.

    Now you do a fake migration. This will sync your database with models.

    python manage.py migrate --fake

    Then run makemigrations again to have rest of the tables created along with a new migration file.

    python manage.py makemigrations

    Regarding your other question, Why makemigrations didn't recogonize your models can be because of reasons like:

    1. Migrations for those changes are already there in some migration file.
    2. You missed it to mention package_name in INSTALLED_APPS but i believe you did it here.
    0 讨论(0)
  • 2021-02-06 19:23

    I tried to edit the related migration file and commented the part where it creates that specific column, then ran python manage.py migrate

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