简体   繁体   English

Django MySQL IntegrityError :(“字段'created_at'没有默认值”)

[英]Django MySQL IntegrityError: (“Field 'created_at' doesn't have a default value”)

I am new to Django. 我是Django的新手。

IntegrityError: (1364, "Field 'created_at' doesn't have a default value") 

occurred when I didn't write created_at for models I defined at models.py. 当我没有为在models.py中定义的模型编写created_at时发生。

Every time I face such error, I add 每次遇到此类错误时,我都会添加

created_at = models.DateTimeField(default=timezone.now, null=True)

to models.py and then run python manage.py makemigrations; python manage.py migrate --fake 到models.py然后运行python manage.py makemigrations; python manage.py migrate --fake python manage.py makemigrations; python manage.py migrate --fake . python manage.py makemigrations; python manage.py migrate --fake

Is that OK? 这可以吗? (I mean, does it follow Django best practice or not? ) When I was using Rails, I've never faced such issues. (我的意思是,它是否遵循Django最佳实践?)当我使用Rails时,我从未遇到过此类问题。 Why doesn't Django automatically handle created_at column? Django为什么不自动处理created_at列?

Also, I'd like to know why --fake option removes this error. 另外,我想知道为什么--fake选项可以消除此错误。

version

Django==1.11.5 
mysqlclient==1.3.12                                                                                                                                                                                                
Python 3.6.1

Thanks in advance. 提前致谢。

It seems that you want to have an attribute created_at which is set on every creation of a model instance. 似乎您想拥有一个在每次创建模型实例时都设置的属性created_at There is an easier way for that: 有一种更简单的方法:

created_at = models.DateTimeField(auto_now_add=True)

Here the docs explaining it in detail https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.DateField.auto_now_add 这里的文档详细解释了它https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.DateField.auto_now_add

From your comments I think I can reproduce your steps. 从您的评论中,我想我可以复制您的步骤。 You had a model class in your models.py : 您在models.py有一个模型类:

from django.db import models

class YourModel(models.Model):
    one_field = models.CharField()
    another_field = models.CharField()

Then you ran: 然后您运行:

python manage.py makemigrations
python manage.py migrate

After that you added to your model class: 之后,您将添加到模型类:

from django.db import models

class YourModel(models.Model):
    one_field = models.CharField()
    another_field = models.CharField()
    created_at = models.DateTimeField(default=timezone.now, null=True)

but forgot to run the migrations and got the error message. 但忘记运行迁移并收到错误消息。

This is because Django ORM is looking for the property created_at , which it can't find in the DB. 这是因为Django ORM正在寻找属性created_at ,该属性在数据库中找不到。 You have to run the commands: 您必须运行以下命令:

python manage.py makemigrations
python manage.py migrate

again. 再次。 Remember that the option --fake won't change the DB. 请记住,选项--fake不会更改数据库。 It just marks the migration as run. 它只是将迁移标记为已运行。 Use it only when you're sure what are you doing. 仅在确定要做什么时才使用它。

As Johannes reichard has suggested you should better use auto_now_add (and there is also auto_now ) for this purpose. 正如Johannes reichard所建议的那样,您最好auto_now_add使用auto_now_add (还有auto_now )。 Check the official documentation . 检查官方文档

The drawback is that this field won't be shown in the admin. 缺点是该字段不会在管理员中显示。 A workaround is to overwrite the save method: 一种解决方法是覆盖save方法:

def save(self, *args, **kwargs):
    if not self.pk:
        self.created_at = timezone.now() # import it from django.utils
    super(YourModel, self).save(*args, **kwargs)

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

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