2016-01-14 86 views
0

我已經在PostgreSQL中創建一個存儲過程如下:org.postgresql.util.PSQLException:ERROR:空值列「的order_id」違反了非空約束

INSERT INTO ABC 

    (order_id,order_dt, customer_id, route_id, routenum, ordertype, create_station_id, create_stationtype, create_time,create_user_id,tran_time, tran_user_id,station_id) 

    values 

    (1,$1, $2, $3, $4, $5, $6, $7, LOCALTIMESTAMP, $8, 

    default, default,$9) 

    returning order_id; 

order_idSERIAL類型 -

PSQLException: ERROR: null value in column "order_id" violates not-null constraint Where: SQL function "insert_ABC" statement 1. 

我使用PostgreSQL 8.2:>primary key

我同時插入如下得到錯誤。這是在我正在做這個主機空間。

我知道錯誤發生,因爲默認類型默認爲空。

什麼是SERIAL類型的default的等效值。

請指導我。

回答

1

你可以跳過INSERT INTOorder_id

CREATE TABLE ABC(order_id SERIAL PRIMARY KEY, order_dt INT -- rest of cols 
       ); 

INSERT INTO ABC(order_dt) -- rest of cols 
VALUES (2)     -- rest of values 
RETURNING order_id; 

或使用default

INSERT INTO ABC(order_id, order_dt) 
VALUES (default, 2) 
RETURNING order_id; 

編輯:

你確定嗎?檢查演示:

CREATE TABLE ABC(order_id SERIAL, order_dt INT); 

INSERT INTO ABC(order_dt) -- rest of cols 
VALUES (2) ; 

INSERT INTO ABC(order_id, order_dt) 
VALUES (default, 2) ; 

SqlFiddleDemo

輸出:

╔═══════════╦══════════╗ 
║ order_id ║ order_dt ║ 
╠═══════════╬══════════╣ 
║  1 ║  2 ║ 
║  2 ║  2 ║ 
╚═══════════╩══════════╝ 

試圖與SQLFiddle執行:

CREATE TABLE ABC(order_id SERIAL NULL, order_dt INT); 
-- ERROR: conflicting NULL/NOT NULL declarations for 
-- column "order_id" of table "abc" 

我沒有環境與PostgreSQL 8.2測試,但如果允許定義ORDER_ID與NULL,你應該改變架構NOT NULL

+0

'default'不會工作,因爲它越來越'NULL'值,這裏'order_id'是一個主鍵,而不是空。 – Santhucool

+0

@Santhucool分享表CREATE SQL。 – lad2025

相關問題