Cannot get Django 1.7 Migrations to detect proper

2019-06-01 18:30发布

问题:

I have a production web project running with a decent amount of data in the MySQL db. I am trying to update the database with some changes to an app called "enterlink." I've made new elements in the existing models and created new models altogether. Before this migration, I have never touched the schema of the db since originally running syncdb to create it. When I run: "python manage.py makemigrations enterlink" the below output appears(pic). My question is, why is this happening? The DB already includes all the models that it lists in the picture so why is it registering those lists of models? When I go to finish the migration by doing "python manage.py migrate" or "python manage.py migrate --fake enterlink" (pic again), I get an output shown but my database schema remains identical to the old db and any new code generates errors. Can anyone tell me what is likely the problem? I would be really appreciative of any advice. It's been very frustrating since I'm not sure what I'm missing.

回答1:

What you have done is that you have ran the command python manage.py syncdb before running python manage.py makemigrations myapp and python manage.py migrate myapp. That is why syncdb created the database schema and the migration was faked because schema already exists. I will suggest to use python manage.py makemigrations myapp and python manage.py migrate myapp and not to use syncdb as its deprecated in Django 1.7.

If you change anything in your model, just run makemigrations and migrate command. Syncdb isn't necessary.



回答2:

This question and relevant answers are intriguing me. Thus I want to share my experience on maintaining live database and migrations.

Tested in django1.5.5

Initializing the database:

  1. ./manage.py syncdb --noinput
  2. ./manage.py migrate
  3. ./manage.py syncdb

Now I have created the database.

Doing a migration for an app:

  1. ./manage.py schemamigration myapp --initial
  2. ./manage.py migrate myapp --fake
  3. Now do necessary changes in your model
  4. ./manage.py schemamigration myapp --auto
  5. ./manage.py migrate myapp


回答3:

Im newbie for schemamigration too, but i will explain how it works for me:

First you create app and then ./manage.py sycndb, so tables are created then you can ./manage.py makemigrations myapp --initial so now initial migrations are created and you should apply them ./manage.py migrate myapp now you can change your models : add,change fields, anything you want and then ./manage.py makemigrations myapp --auto this will create migrations for changes and now you need to apply them enter code here./manage.py migrate myapp so this actually will create new tables in db