如何修复 NEXTVAL 在插入查询中返回 null 但在 PostgreSQL 中单独执行时 returns 正确值?
How to fix NEXTVAL returning null in insert query but returns correct value when executed alone in PostgreSQL?
当我执行以下插入查询时:
insert into FEED_STAGING_GOOD_TRADE (JOBEXECUTIONID, STAGINGID, ROWNUMBER, system, traderef, mtmvaluation, mtmvaluationccy, prcmtmquality, prcdate)
values (64, NEXTVAL('FEED_STAGING_TRADE_SEQ') ,2,'RMS','TRD3',1.11111111E8,'USD',100.0,'2011-12-09 +00'::timestamp)
我得到以下输出:
ERROR: null value in column "tradestagingid" violates not-null constraint
Detail: Failing row contains (46, 64, null, 2, null, null, null, RMS, null, null, TRD3, null, null, null, null, null, null, null, null, null, null, null, null, 111111111, USD, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, 100, 2011-12-09 00:00:00, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null).
应该由NEXTVAL
函数结果填充的第三列被发送为空,这违反了某个非空约束。
然而,当我像这样独立执行NEXTVAL
时:
select NEXTVAL('FEED_STAGING_TRADE_SEQ');
函数returns正确值如下截图所示:
您的插入内容为 stagingid
列提供了值,但没有为 tradestagingid
列(错误消息所指)提供值
您的 INSERT 语句甚至没有列出名为 tradestagingid
的列,因此将使用该列的默认值。
正如您似乎希望 tradestagingid
填充序列值一样,您需要更改 INSERT 的列列表并使用 tradestagingid
而不是 stagingid
。或者更好:将其定义为标识列,以便自动填充。
当我执行以下插入查询时:
insert into FEED_STAGING_GOOD_TRADE (JOBEXECUTIONID, STAGINGID, ROWNUMBER, system, traderef, mtmvaluation, mtmvaluationccy, prcmtmquality, prcdate)
values (64, NEXTVAL('FEED_STAGING_TRADE_SEQ') ,2,'RMS','TRD3',1.11111111E8,'USD',100.0,'2011-12-09 +00'::timestamp)
我得到以下输出:
ERROR: null value in column "tradestagingid" violates not-null constraint
Detail: Failing row contains (46, 64, null, 2, null, null, null, RMS, null, null, TRD3, null, null, null, null, null, null, null, null, null, null, null, null, 111111111, USD, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, 100, 2011-12-09 00:00:00, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null).
应该由NEXTVAL
函数结果填充的第三列被发送为空,这违反了某个非空约束。
然而,当我像这样独立执行NEXTVAL
时:
select NEXTVAL('FEED_STAGING_TRADE_SEQ');
函数returns正确值如下截图所示:
您的插入内容为 stagingid
列提供了值,但没有为 tradestagingid
列(错误消息所指)提供值
您的 INSERT 语句甚至没有列出名为 tradestagingid
的列,因此将使用该列的默认值。
正如您似乎希望 tradestagingid
填充序列值一样,您需要更改 INSERT 的列列表并使用 tradestagingid
而不是 stagingid
。或者更好:将其定义为标识列,以便自动填充。