我收到这样的错误:
/ book_table /中的IntegrityError “resv_id”列中的空值违反非空约束 详细信息:失败的行包含(null,2015-01-03,bb,23,aa @ aa.com,null,e,3,null)。
resv_id字段,它是我的数据库的主键字段,类型为serial。
我这样声明:
CREATE TABLE "Reservation"
(
resv_id serial NOT NULL,
resv_date date,
resv_customer_name character varying(255),
resv_customer_phone character varying(20),
resv_customer_email character varying(255),
resv_time integer, -- This value can only be an int value that goes from 0 to 23.
resv_special_requests character varying(300),
resv_no_ppl integer,
resv_table integer,
CONSTRAINT "Reservation_pkey" PRIMARY KEY (resv_id),
CONSTRAINT resv_table_id FOREIGN KEY (resv_table)
REFERENCES tables (table_number) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
此外,预订模型如下所示:
class Reservation(models.Model):
resv_id = models.IntegerField(primary_key=True)
resv_date = models.DateField(blank=True, null=True)
resv_customer_name = models.CharField(max_length=255, blank=True)
resv_customer_phone = models.CharField(max_length=20, blank=True)
resv_customer_email = models.CharField(max_length=255, blank=True)
resv_time = models.IntegerField(blank=True, null=True)
resv_special_requests = models.CharField(max_length=300, blank=True)
resv_no_ppl = models.IntegerField(blank=True, null=True)
resv_table = models.ForeignKey('Tables', db_column='resv_table', blank=True, null=True)
class Meta:
managed = False
db_table = 'Reservation'
我查看了Django文档中的自定义主键字段,看起来我已经做好了一切,但出于某种原因,我不断得到上述错误,除非我在创建模型的新实例时给resv_id字段赋值使用objects.create方法。
你们知道为什么会这样吗?
答案 0 :(得分:0)
当您创建新实例但未通过resv_id
时,IntegrityError
会导致resv_id
,因为resv_id = models.AutoField(primary_key=True)
是主键并且不允许空。我不知道你为什么要这样做,因为所有django模型都已经有id field版本,但你可以尝试这样的事情:{{1}}