[英]How to fix NEXTVAL returning null in insert query but returns correct value when executed alone in PostgreSQL?
When I execute the following insert query:当我执行以下插入查询时:
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)
I get the following output:我得到以下输出:
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).
The third column which is supposed to be filled by the NEXTVAL
function result is being sent null, which is violating a certain non-nullity constraint.应该由
NEXTVAL
函数结果填充的第三列被发送为空,这违反了某个非空约束。
However when I execute NEXTVAL
independently like this:但是,当我像这样独立执行
NEXTVAL
时:
select NEXTVAL('FEED_STAGING_TRADE_SEQ');
The function returns a correct value as shown below in the screenshot:该函数返回正确的值,如下面的屏幕截图所示:
Your insert provides a value for the column stagingid
, but not for the column tradestagingid
(to which the error message refers)您的插入为列
stagingid
提供了一个值,但没有为列tradestagingid
(错误消息所指的)提供一个值
You INSERT statement doesn't even list a column named tradestagingid
so the default value for that column will be used.您的 INSERT 语句甚至没有列出名为
tradestagingid
的列,因此将使用该列的默认值。
As you seem to expect the tradestagingid
to be populated with the sequence value, you need to change the column list of your INSERT and use tradestagingid
instead of stagingid
.由于您似乎希望用序列值填充
tradestagingid
,因此您需要更改 INSERT 的列列表并使用tradestagingid
而不是stagingid
。 Or better: define it as an identity column, so it gets populated automatically.或者更好:将其定义为标识列,以便自动填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.