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

Ruby 错误:列“中的值为空”;id";违反非空约束,ruby,postgresql,factory-bot,Ruby,Postgresql,Factory Bot,我刚刚将我的应用程序从mysql迁移到postgres,但当我尝试在特定表中插入记录时,我得到的违反了非空约束错误: 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

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

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\u 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, 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,知道吗?

您必须跳过
插入操作中的
id

插入工作分配(帐户id、l\U id等)
价值观
(1, 1, ...)

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

从列列表中删除
id
,并从值列表中前导
null
。Autoincrement弱于直接显式赋值。请显示您的
赋值
工厂。实际上rails中的ActiveRecord查询传递空参数,需要检查是否可以排除该参数。