错误:"id"列中的值为空违反非空约束

编程入门 行业动态 更新时间:2024-10-28 13:29:25
本文介绍了错误:"id"列中的值为空违反非空约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我只是将我的应用程序从 mysql 迁移到了 postgres ,但是当我尝试在特定表中插入记录时,我得到了违反非空约束

I just migrated my app from mysql to postgres but when I try to insert a record in a specific table I get violates not-null constraint error:

ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null, 1, 1, null, null, null, 2016-03-09 09:24:12.841891, 2012-12-31 23:00:00, 2012-12-31 23:00:00, null, null, f, null, f, XYZAssignment, null, null, null, null). ********** Error ********** ERROR: null value in column "id" violates not-null constraint SQL state: 23502 Detail: Failing row contains (null, 1, 1, null, null, null, 2016-03-09 09:24:12.841891, 2012-12-31 23:00:00, 2012-12-31 23:00:00, null, null, f, null, f, XYZAssignment, null, null, null, null).

当我尝试使用 factory_girl 创建记录时:

When I try to create the record using factory_girl:

@assignment = FactoryGirl.create(:assignment)

它建立以下sql查询:

It builds this sql query:

INSERT INTO assignments( id, account_id, l_id, viewed_at, accepted_at, declined_at, expires_at, created_at, updated_at, decline_reason, decline_reason_text, promotion, c_checked_at, forwardable, type, f_promo, c_check_successful, c_check_api_result, c_check_human_result) VALUES (null, 1, 1, null, null, null, '2016-03-09 09:24:12.841891', '2012-12-31 23:00:00', '2012-12-31 23:00:00', null, null, 'f', null, 'f', 'XYZAssignment', null, null, null, null);

这是分配工厂:

FactoryGirl.define do factory :assignment do expires_at 24.hours.from_now account lead end end

这是表格说明:

CREATE TABLE assignments( id serial NOT NULL, account_id integer NOT NULL, l_id integer NOT NULL, viewed_at timestamp without time zone, accepted_at timestamp without time zone, declined_at timestamp without time zone, expires_at timestamp without time zone, created_at timestamp without time zone, updated_at timestamp without time zone, decline_reason character varying(16), decline_reason_text character varying(256), promotion boolean NOT NULL DEFAULT false, c_checked_at timestamp without time zone, forwardable boolean DEFAULT true, type character varying(64), f_promo boolean, c_check_successful boolean, c_check_api_result character varying(32), c_check_human_result character varying(32), CONSTRAINT assignments_pkey PRIMARY KEY (id) ) WITH ( OIDS=FALSE );

看起来无法自动增加ID,知道吗?

Looks its not able to auto increment the id, any idea?

推荐答案

您必须在 INSERT 操作中跳过 id :

INSERT INTO assignments(account_id, l_id, ...) VALUES (1, 1, ...)

id 将自动获得下一个序列号,因为它是一个自动递增的字段.

The id will automatically get the next sequence number, since it is an auto-increment field.

更多推荐

错误:"id"列中的值为空违反非空约束

本文发布于:2023-10-22 20:54:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:值为   错误   quot   id

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!