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
之后,我得到以下信息:
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
即使我按 1 并尝试设置一个随机的一次性值,它也会成功迁移,但后来网站因 "no such table: blog_posts" 而崩溃。但我认为它应该可以在没有手动设置默认值等变通方法的情况下工作。
我尝试使用帖子和答案的主键。我尝试完全删除它们,以便 django 自动设置它们并尝试将其从 AutoField 更改为 UUIDField,反之亦然,但它没有帮助。我做错了什么?
您看到此错误消息是因为 django 正在尝试建立一个一致的迁移历史记录,并且它抱怨说如果有一个数据库保存了您的旧迁移的数据,并且您尝试添加一个非- 可为空的字段,它不知道该做什么。
迁移应该放入版本控制中并在不同的 development/production 环境中使用。如果您添加一个不能为空的字段,其他环境的现有数据(例如,生产数据库包含没有字段 post_id
的模型),那么 django 会警告您这一点,并显示错误消息你得到了,并提供了两种解决方案:
此字段应始终预填充默认值(您必须修改 models.py)
这是一次性迁移,您提供一次性值,例如 "LEGACY" 以标记迁移前数据。
如果您不在生产环境中并且您的开发服务器上没有有价值的数据,修复此错误消息的一种简单方法是删除现有的迁移文件和 运行 python manage.py makemigrations && python manage.py migrate
再次.
我正在尝试在 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
之后,我得到以下信息:
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
即使我按 1 并尝试设置一个随机的一次性值,它也会成功迁移,但后来网站因 "no such table: blog_posts" 而崩溃。但我认为它应该可以在没有手动设置默认值等变通方法的情况下工作。
我尝试使用帖子和答案的主键。我尝试完全删除它们,以便 django 自动设置它们并尝试将其从 AutoField 更改为 UUIDField,反之亦然,但它没有帮助。我做错了什么?
您看到此错误消息是因为 django 正在尝试建立一个一致的迁移历史记录,并且它抱怨说如果有一个数据库保存了您的旧迁移的数据,并且您尝试添加一个非- 可为空的字段,它不知道该做什么。
迁移应该放入版本控制中并在不同的 development/production 环境中使用。如果您添加一个不能为空的字段,其他环境的现有数据(例如,生产数据库包含没有字段 post_id
的模型),那么 django 会警告您这一点,并显示错误消息你得到了,并提供了两种解决方案:
此字段应始终预填充默认值(您必须修改 models.py)
这是一次性迁移,您提供一次性值,例如 "LEGACY" 以标记迁移前数据。
如果您不在生产环境中并且您的开发服务器上没有有价值的数据,修复此错误消息的一种简单方法是删除现有的迁移文件和 运行 python manage.py makemigrations && python manage.py migrate
再次.