Django 1.7 migrations won't recreate a dropped

2019-01-31 00:39发布

Using Django 1.7 migrations.

I accidentally dropped a table in my database. I assumed that by running migration again this would recreate the table but no, Django states "No migrations to apply".

How to I get Django to recreate the table?

I have run:

> makemigrations - No changes detected
> migrate - No migrations to apply.

I have tried making a change to the model and running a new migration and it simply states that "Table 'x.test_customer' doesn't exist" which is correct, but what I was hoping it that it would recreate the table.

9条回答
家丑人穷心不美
2楼-- · 2019-01-31 01:04

In my case in django 2.0.2 for recreating dropped table I needed to comment my models in myapp and then migrate with --fake and uncomment my models and migrate without --fake A little different from raul answer:

  1. Delete your migrations files in your desired app
  2. Thanks to raul answer: In the database: DELETE FROM django_migrations WHERE app = 'app_name'.
  3. comment codes in models.py and all this models usage in views, signals and etc (to prevent error).
  4. python manage.py makemigrations YOUR_APP_NAME
  5. python manage.py migrate --fake
  6. un-comment what you commented in step 3
  7. python manage.py makemigrations YOUR_APP_NAME
  8. migrate without --fake: python manage.py migrate

This should solve some users problem.

查看更多
聊天终结者
3楼-- · 2019-01-31 01:05

Another solution I've found and works perfectly:

In django 1.7:

  1. Delete your migrations folder

  2. In the database: DELETE FROM django_migrations WHERE app = 'app_name'.

    You could alternatively just truncate this table.

  3. python manage.py makemigrations

  4. python manage.py migrate --fake

In django 1.9.5:

  1. Delete your migrations folder
  2. In the database: DELETE FROM django_migrations WHERE app = 'app_name'.

    You could alternatively just truncate this table.

  3. python manage.py makemigrations app_name

  4. python manage.py migrate

This works 100% for me!

查看更多
Explosion°爆炸
4楼-- · 2019-01-31 01:08

Go to your database and find the table django_migrations. Delete all the rows which have app equals your app name.

Then do a makemigrations & migrate will work.

查看更多
登录 后发表回答