Django 1.7迁移不会重新创建一个丢弃的表,为什么? [英] Django 1.7 migrations won't recreate a dropped table, why?

查看:98
本文介绍了Django 1.7迁移不会重新创建一个丢弃的表,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Django 1.7迁移。



我不小心在我的数据库中删除了一个表。我假设通过运行迁移,这将重新创建表,但不,Django说没有迁移应用。



如何让Django重新创建表? / p>

我已经运行:

 > makemigrations  - 没有检测到变化
> migrate - 不应用迁移。

我已经尝试对模型进行更改并运行新的迁移,它只是说表'x.test_customer'不存在这是正确的,但是我希望它可以重新创建表。

解决方案

p>迁移检查模型中的差异,然后将其转换为被转换为SQL的操作。它自动将db方案与模型同步,并且无法知道您删除了表(它不知道手动更改,因为,您不应该做手动更改。这是重点)



答案?手动更改还需要手动迁移。您需要做的只是编写自己的迁移,并手动告诉南方重新构建表。这不是很难,文档使其变得非常简单。只需这样做:

 从django.db导入迁移,模型

类迁移(迁移。

operations = [
migrations.CreateModel(Foo),
migrations.AddField(Foo,bar,models.IntegerField(default = 0))
]

您可以查看第一个迁移文件使模型在第一位),并复制粘贴几乎所有的。那么你所要做的就是像你一样执行迁移。


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.

解决方案

Migrations check for differences in your models, then translates that to actions, which are translated to SQL. It does not automatically sync the db scheme with your models, and it has no way of knowing you dropped a table (it doesn't know about manual changes because, well, you're not supposed to do manual changes. That's the point)

The answer? a manual change requires a manual migration as well. What you need to do is simply write your own migration and manually tell south to re-build the table. It's not very difficult, The docs make it pretty easy. Just make something like this:

from django.db import migrations, models

class Migration(migrations.Migration):

    operations = [
        migrations.CreateModel("Foo"),
        migrations.AddField("Foo", "bar", models.IntegerField(default=0))
    ] 

You can probably look into the first migration file (the one that made the model in the first place) and copy paste almost all of it. Then all you have to do is run the migration like you always do

这篇关于Django 1.7迁移不会重新创建一个丢弃的表,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆