I am learning django these days, the project is to do my own blog, and then the shell command to create a new blog when the following error
django.db.utils.IntegrityError: NOT NULL constraint failed: blog_blog.author_id
is known from the error message that the non-null constraint of the database is the problem, and it is in blog.auther_id that the problem is
and then look at my creation process, first go back to my models model, the source code is as follows
from django.db import models
from django.contrib.auth.models import User
# 创建博客分类模型
class BlogType(models.Model):
type_name = models.CharField(max_length=15)
def __str__(self):
return self.type_name
# 创建博客模型
class Blog(models.Model):
title = models.CharField(max_length=50)
blog_type = models.ForeignKey(BlogType, on_delete=models.DO_NOTHING)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.DO_NOTHING)
created_time = models.DateTimeField(null=True, auto_now_add=True)
last_updated_time = models.DateTimeField(null=True, auto_now=True)
def __str__(self):
return "<Blog: %s>" % self.title
can tell me the foreign key I used in the author, and the author class I didn’t define in the model, so my shell code is as follows:
> > > from django.contrib.auth.models import User
> > > User.objects.all()
< QuerySet [<User: xxz>] >
> > > blog.auther = User.objects.all()[0]
> > > blog.save()
sqlite3.IntegrityError: NOT NULL constraint failed: blog_blog.author_id
and then there’s the problem, and the solution to that is very simple, is just to type
again
> > > blog.author_id = 1
> > > blog.save()
problem solved, so now let’s talk about how the problem came to be
p>
https://docs.djangoproject.com/en/1.11/ref/models/instances/
here in the django document is the sentence
Model. code>
pk code> ¶ p>
Regardless of whether you define a primary key field yourself, or let Django supply one for you, each model will have a property called pk
. It behaves like a normal attribute on the model, but is actually an alias for whichever attribute is the primary key field for the model. You can read and set this value, just as you would for any other attribute, and it will update the correct field in the model.
translation is
whether you define your own primary key field or let Django provide one for you, each model has a property called pk. It behaves like a normal property on the model, but is actually an alias for an alias that belongs to the model's primary key field. You can read and set this value as you would any other property, and it updates the correct fields in the model.
general meaning is that you defined in the models of all the classes (as a table in the database), there will be a primary key id, every time you create an object, like write blog = blog () above, will automatically assign an id in the blog, and the increment, by the same token, the auther in the blog, to define a id, but not defined before, so this manual are defined.
p>
p>
p>
p>
p>
p>
p>
p>
Read More:
- django.db.utils.IntegrityError: UNIQUE constraint failed: auth_user.username
- IntegrityError at ** NOT NULL constraint failed: learning_logs_topic.owner_id
- django.db.utils.OperationalError: no such table: django_admin_log
- Django. DB. Utils. Programmingerror: (1146, “table * doesn’t exist”) causes and Solutions
- A little bug of CSDN blog
- python sqlite Error IntegrityError: UNIQUE constraint failed: table_area1.user_name
- 404 error in accessing GitHub personal blog
- How to solve the problem of error reporting in the introduction of Python Django no module name ‘Django. Utils. Six’
- Halo blog system reports log error when idea is running
- django.core.exceptions . improveconfigured: error loading MySQL DB module. Django configuring MySQL database
- Parse error: syntax error, unexpected T_OBJECT_OPERATOR in E:\WWW\blog\hyii2\frontend\web\index.php
- Failed to add the foreign key constraint. Missing index for constraint ‘stu_ibfk_1’ in the reference
- 1822 – Failed to add the foreign key constraint. Missing index for constraint ‘tb_emp_ibfk_1’ in the
- greendao insert data UNIQUE constraint failed: PURCHASE_ORDER_TABLE._id (code 1555)
- Spring security failed to log in, error: there is no passwordencoder mapped for the ID “null”
- Solve the problem of error loading MySQL DB module. Encountered during Django project
- Uncaught typeerror: cannot set property ‘of null error resolution
- Apple mach-o linker (ID) error resolution
- There is a solution to the problem: severity = corrected, type = physical layer, id = 00e5 or id = 00e8 (receiver ID) under Ubuntu
- Microsoft fix 50688 [Windows 7 event ID10, WMI error resolution