简体   繁体   English

Django 在迁移期间要求默认值

[英]Django asking for default value during migration

I've got the following models:我有以下型号:

    title = models.CharField(max_length=100)
    description = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.pk})


class Ingredient(models.Model):
    ingredients_recipe = models.ForeignKey(Post, on_delete=models.CASCADE)
    type = models.CharField(max_length=50)
    quantity = models.IntegerField()
    measurement = models.CharField(max_length=50)


class MethodStep(models.Model):
    methods_recipe = models.ForeignKey(Post, on_delete=models.CASCADE, null=True)
    step = models.TextField()

When i try to run makemigrations , i get no errors for the Ingredient class, but on the MethodStep class i get an error saying:当我尝试运行makemigrations时,我没有收到Ingredient class 的错误,但在MethodStep class 上我收到错误消息:

You are trying to add a non-nullable field 'methods_recipe' to methodstep without a default;您正在尝试将不可为空的字段“methods_recipe”添加到没有默认值的方法步骤; we can't do that (the database needs something to populate existing rows).我们不能这样做(数据库需要一些东西来填充现有的行)。

Can someone help me?有人能帮我吗? I'm not sure whats different about each of these classes which would mean the MethodStep has an error but Ingredient doesn't.我不确定这些类中的每一个有什么不同,这意味着MethodStep有错误,但Ingredient没有。

Edit: I've also tried deleting/clearing/removing the db but the error persists everytime makemigrations is run.编辑:我也尝试过删除/清除/删除数据库,但每次运行makemigrations时错误仍然存在。

Sometimes in addition to resetting the database you will also have to remove all existing Migrations from your project (or at least the ones for the package these models belong to).有时除了重置数据库之外,您还必须从项目中删除所有现有的迁移(或者至少是这些模型所属的 package 的迁移)。

Delete all files in the migrations folder EXCEPT for the _init__.py file.删除迁移文件夹中的所有文件,除了 _init__.py 文件。

When simply dumping your db and restarting django, startup will apply the migration history in the order they were created.当简单地转储您的数据库并重新启动 django 时,启动将按照创建顺序应用迁移历史记录。

That means if you added this ForeignKey Field after already migrating changes to the db, django won't let you migrate without a default unless you delete the migration history.这意味着如果您在将更改迁移到数据库之后添加了此 ForeignKey 字段,则 django 不会让您在没有默认值的情况下进行迁移,除非您删除迁移历史记录。

This happens after you create a model field.这发生在您创建 model 字段之后。

This is because you have added rows/objects in the table/model since your last migration.这是因为自上次迁移以来,您在表/模型中添加了行/对象。

Django is aware of table rows in your db and and cannot figure out what to fill the new field with for the old rows. Django 知道数据库中的表行,并且无法弄清楚为旧行填充新字段的内容。 To fix this, depending on the type of field you created newly, update the field adding: default=None :if you don't mind those fields having nothing to go with otherwise consider some default string or int or float consistent with the field going forward AND also add blank = True要解决此问题,根据您新创建的字段类型,更新字段添加: default=None :如果您不介意那些与 go 无关的字段,否则考虑一些默认字符串或 int 或 float 与该字段一致向前 AND 还添加空白 = True

To take everything back to square one, apart from deleting your db, you will also need to delete associated migration files.要将一切恢复原状,除了删除数据库之外,您还需要删除相关的迁移文件。 If you are not sure which it is, delete all of them but do not touch the init file in the folder.如果您不确定它是哪个,请将它们全部删除,但不要触摸文件夹中的init文件。 Then makemigrations and migrate.然后进行迁移和迁移。

PS: I'm sure you know that with the last approach you lose all records in the model. PS:我确定您知道使用最后一种方法会丢失 model 中的所有记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM