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:
- Importerror of Django error: no module named**
- IntegrityError at ** NOT NULL constraint failed: learning_logs_topic.owner_id
- ERROR 1048 (23000): Column ‘courseno‘ cannot be null
- DB2 create table error – 104 42601 illegal symbol encountered in SQL statement
- Pg_dump Error: pg_dump: No matching tables were found,pg_dump: schema with OID 1515227 does not exi
- Failed to add the foreign key constraint. Missing index for constraint ‘stu_ibfk_1’ in the reference
- django.core.exceptions . improveconfigured: error loading MySQL DB module. Django configuring MySQL database
- nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property
- javax.persistence.EntityNotFoundException : unable to find error
- org.apache.ibatis.type.TypeException: Error setting non null for parameter #4 with JdbcType null
- django.db.utils.OperationalError: no such table: django_admin_log
- Modification method of starting value of MySQL auto increment ID
- 1822 – Failed to add the foreign key constraint. Missing index for constraint ‘tb_emp_ibfk_1’ in the
- assigning to rvalue
- The solution of Hibernate query returning all null lists
- mysql ERROR 1025 (HY000): Error on rename of
- SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
- MySQL failed to add foreign key error 1452
- MySQL error: 1005 can’t create table (error: 150)
- Solution of duplicate entry ‘value’ for key ‘field name’ in MySQL