是否保证INSERT RETURNING返回“正确”的内容?订购? [英] Is INSERT RETURNING guaranteed to return things in the "right" order?

查看:112
本文介绍了是否保证INSERT RETURNING返回“正确”的内容?订购?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

create table foo(
    id serial, 
    txt text
);

insert into foo(txt) values ('a'),('b'),('c') returning id;

返回值:

 id 
----
  1
  2
  3
(3 rows)

似乎 第一 id 返回值将始终是'a' id ,第二个是'b的第二个',依此类推,但这是插入的定义行为,还是偶然的情况,可能在奇怪的情况下失败?

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 订单将与 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆