2016-03-08 246 views
4

我只是遷移我的應用程序從mysqlpostgres但是當我嘗試在一個特定的表中插入一條記錄,我得到violates not-null constraint錯誤:錯誤:在列「ID」空值違反非空約束

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創造了紀錄:

@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,任何想法?

+0

從列表中刪除'id'並從值列表中導出'null'。自動增量比直接顯式賦值弱。 – mudasobwa

+0

請出示你的作業工廠。 – spickermann

回答

1

你要跳過idINSERT操作:

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

id將自動獲得下一個序列號,因爲它是一個自動遞增字段。

+1

實際上,rails中的ActiveRecord查詢傳遞了null參數,需要檢查我是否可以排除該參數。 – tokhi

相關問題