ruby – 错误:列“id”中的空值违反了非空约束

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

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

********** Error **********

    ERROR: null value in column "id" violates not-null constraint
    sql state: 23502
    Detail: Failing row contains (null,null).

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

@assignment = FactoryGirl.create(:assignment)

它构建了这个SQL查询

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,'2016-03-09 09:24:12.841891','2012-12-31 23:00:00','f','XYZAssignment',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,任何想法?

解决方法

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

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

id将自动获取一个序列号,因为它是一个自动增量字段.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...