INSERT RETURNING 是否保证返回“正确"的东西?命令? [英] Is INSERT RETURNING guaranteed to return things in the "right" order?
问题描述
例子:
create table foo(
id serial,
txt text
);
insert into foo(txt) values ('a'),('b'),('c') returning id;
返回:
id
----
1
2
3
(3 rows)
似乎返回值中的 first id
将始终是 的
,第二个是 id
'a''b'
以此类推,但这是 insert into
的定义行为,还是可能会失败的巧合奇怪的情况?
It seems that the first id
in the return value will always be the id
for 'a'
, the second for 'b'
and so on, but is this defined behaviour of insert into
, or is it a coincidence that may fail under odd circumstances?
推荐答案
我在 保证 RETURNING
订单的文档 所以我认为你不能依赖它.奇怪的是 RETURNING
顺序将匹配 VALUES
顺序,但我看不到任何关于 VALUES
将插入哪个顺序的保证;VALUES
几乎肯定会按从左到右的顺序插入,但同样没有书面保证.
I don't see anything in the documentation that guarantees an order for RETURNING
so I don't think you can depend on it. Odds are that the RETURNING
order will match the VALUES
order but I don't see any guarantees about what order the VALUES
will be inserted in either; the VALUES
are almost certainly going to be insert in order from left to right but again, there is no documented guarantee.
此外,关系模型是基于设置的,因此排序是用户应用的东西,而不是关系的固有属性.一般来说,如果无法明确指定排序,则没有隐含排序.
Also, the relational model is set based so ordering is something applied by the user rather than an inherent property of a relation. In general, if there is no way to explicitly specify an ordering, there is no implied ordering.
执行摘要:您所看到的顺序可能总是会发生,但不能保证,所以不要依赖它.
Execute summary: the ordering you're seeing is probably what will always happen but it is not guaranteed so don't depend on it.
这篇关于INSERT RETURNING 是否保证返回“正确"的东西?命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!