所以,我正在写迁移。
我的目标是:
source
表移至dest
表。 (将数据插入dest
,然后更新source
中的引用,将dest_id
设置为id
表中刚刚创建的行的dest
< / LI>
source
表格移动的行。我对第2步遇到了困难。有任何想法吗?
我目前有这个:
do $$
declare os_id bigint;
begin
insert into order_shipments (order_id, method_id, deliver_at, barcode, sent_at, city_id, subway_id)
select o.id, o.shipment_method_id, o.courier_deliver_at, o.barcode, o.sent_at, o.courier_city_id, o.courier_subway_id
from orders o
returning id into os_id;
end
$$;
update orders as o set current_shipment_id = os_id;
我正在创建匿名阻止,该阻止从orders
(source
)获取数据并将其插入order_shipments
(dest
)。
答案 0 :(得分:0)
刚刚找到了相当简单的解决方案。
感谢问题Insert data and set foreign keys with Postgres
结果是
with i as (
insert into order_shipments (order_id, method_id, deliver_at, barcode, sent_at, city_id, subway_id)
select o.id, o.shipment_method_id, o.courier_deliver_at, o.barcode, o.sent_at, o.courier_city_id, o.courier_subway_id
from orders o
returning id
)
update orders o
set current_shipment_id = i.id
from i;