Django迁移(Django migrations)

编程入门 行业动态 更新时间:2024-10-22 18:26:06
Django迁移(Django migrations)

我正在尝试在django上建立一个博客。 我已经创造了模型。 他们来了:

from django.db import models import uuid class Users(models.Model): username = models.CharField(max_length = 32, primary_key = True) password = models.CharField(max_length = 32) email = models.EmailField() registration_date = models.DateTimeField(auto_now_add = True) class Posts(models.Model): author = models.ForeignKey("Users") header = models.CharField(max_length=100) body = models.TextField() pub_date = models.DateTimeField(auto_now_add = True) mod_date = models.DateTimeField(auto_now = True) upvotes = models.PositiveIntegerField() views = models.PositiveIntegerField() post_id = models.AutoField(primary_key = True) class Answers(models.Model): body = models.TextField() author = models.ForeignKey("Users") pub_date = models.DateTimeField(auto_now_add = True) mod_date = models.DateTimeField(auto_now = True) post = models.ForeignKey("Posts") answer_id = models.UUIDField(primary_key = True, default=uuid.uuid4)

运行python manage.py migrate我得到以下内容:

您试图在没有默认值的帖子中添加不可为空的字段“post_id”; 我们不能那样做(数据库需要用法来填充现有的行)。 请选择一个修复: 1)现在提供一次性默认值(将在所有现有行上设置) 2)退出,让我在models.py中添加默认值

即使我按1并尝试设置一个随机的一次性值,它也会成功迁移,但稍后在网站上崩溃时会出现“没有这样的表:blog_posts”。 但我认为它应该没有这样的解决方法,无论如何手动设置默认值。

我尝试使用帖子和答案的主键。 我尝试完全删除它们,以便django自动设置它们并尝试将它从AutoField更改为UUIDField,反之亦然,但它没有帮助。 我究竟做错了什么?

I am trying to build a blog on django. I have gone as far as creating models. Here they are:

from django.db import models import uuid class Users(models.Model): username = models.CharField(max_length = 32, primary_key = True) password = models.CharField(max_length = 32) email = models.EmailField() registration_date = models.DateTimeField(auto_now_add = True) class Posts(models.Model): author = models.ForeignKey("Users") header = models.CharField(max_length=100) body = models.TextField() pub_date = models.DateTimeField(auto_now_add = True) mod_date = models.DateTimeField(auto_now = True) upvotes = models.PositiveIntegerField() views = models.PositiveIntegerField() post_id = models.AutoField(primary_key = True) class Answers(models.Model): body = models.TextField() author = models.ForeignKey("Users") pub_date = models.DateTimeField(auto_now_add = True) mod_date = models.DateTimeField(auto_now = True) post = models.ForeignKey("Posts") answer_id = models.UUIDField(primary_key = True, default=uuid.uuid4)

After running python manage.py migrate I get the following:

You are trying to add a non-nullable field 'post_id' to posts without a default; we can't do that (the database need mething to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows) 2) Quit, and let me add a default in models.py

Even if I press 1 and try to set a random one-off value, it migrates successfully but later on the website crashes with "no such table: blog_posts". But I think it should work without such workarounds as setting the default value manually anyway.

I tried playing with primary keys for Posts and Answers. I tried completely removing them so that django automatically sets them itself and tried changing it from AutoField to UUIDField and vice versa but it didn't help. What am I doing wrong?

最满意答案

您看到此错误消息是因为django正在尝试构建一致的迁移历史记录,并且它抱怨如果有一个数据库用旧迁移保存数据,并且您尝试添加一个不可为空的字段,那么不知道该怎么办。

迁移应该放在版本控制中,并在不同的开发/生产环境中使用。 如果你添加一个不能为空的字段,其他环境的现有数据(例如一个生产数据库,其中包含没有字段post_id ),那么django会警告你这个,并带有你得到的错误信息,提供两种解决方案

此字段应始终使用默认值进行预先设定(您必须修改models.py)

这是一次性迁移,您提供一次性值,例如“LEGACY”以标记迁移前数据。

如果您尚未投入生产并且开发服务器上没有有价值的数据,则修复此错误消息的一种简单方法就是删除现有的迁移文件并再次运行python manage.py makemigrations && python manage.py migrate 。

You're seeing this error message because django is trying to build a consistent history of migrations, and it complains that if there was a database that held data with your old migrations, and you'd try to add a non-nullable field, it wouldn't know what to do.

Migrations are supposed to be put into version control and used across different development/production environments. If you add a field that must not be null, existing data of other environments (for example a production database that held models that did not have the field post_id) then django will warn you about this, with the error message that you got, and offer two solutions:

This field should always be prepoulated with a default value( you have to modify the models.py)

This is a one time migration and you supply a one-off value, for example "LEGACY" to mark pre-migration data.

If you're not in production and there is no valuable data on your development server, an easy way to fix this error message is just to delete the existing migration files and run python manage.py makemigrations && python manage.py migrate again.

更多推荐

本文发布于:2023-08-07 07:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462310.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Django   migrations

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!