Django migrations : relation already exists

丶灬走出姿态 提交于 2019-12-04 20:33:33

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.

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)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!