简体   繁体   English

Django迁移

[英]Django migrations

I am trying to build a blog on django. 我正在尝试在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: 运行python manage.py migrate我得到以下内容:

You are trying to add a non-nullable field 'post_id' to posts without a default; 您正尝试在没有默认值的帖子中添加不可为空的字段“post_id”; 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) 1)现在提供一次性默认值(将在所有现有行上设置)
2) Quit, and let me add a default in models.py 2)退出,让我在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". 即使我按1并尝试设置一个随机的一次性值,它也会成功迁移,但稍后在网站上崩溃时会出现“没有这样的表: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. 我尝试完全删除它们,以便django自动设置它们并尝试将其从AutoField更改为UUIDField,反之亦然,但它没有帮助。 What am I doing wrong? 我究竟做错了什么?

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

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: 如果你添加一个不能为空的字段,其他环境的现有数据(例如一个生产数据库,其中包含没有字段post_id ),那么django会警告你这个,并带有你得到的错误信息,以及提供两种解决方案

  1. This field should always be prepoulated with a default value( you have to modify the models.py) 此字段应始终预先设置为默认值(您必须修改models.py)

  2. This is a one time migration and you supply a one-off value, for example "LEGACY" to mark pre-migration data. 这是一次性迁移,您提供一次性值,例如“LEGACY”以标记迁移前数据。

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

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

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