假设您有以下表结构,您希望维基百科具有存储在不同表中的页面的标识和状态:
create table endUsers (
uuid UUID primary key,
created timestamptz default now()
);
create table endUserRevisions (
id bigserial primary key,
endUser UUID not null references endUsers,
modified timestamptz default now(),
modifiedBy UUID not null references portalUsers,
name text not null,
company text not null,
email text not null
);
alter table endUsers add column
latestRevision bigint not null references endUserRevisions;
然后您想要将全新的用户插入此数据库,如:
with lastID as (
insert into endUserRevisions (endUser, name, company, email)
values ('08e7882c-7596-43d1-b4cc-69f855210d72', 'a', 'b', 'c') returning id)
insert into endUsers (uuid, latestRevision)
values ('08e7882c-7596-43d1-b4cc-69f855210d72', lastID);
-- or
with revision as (
insert into endUserRevisions (endUser, name, company, email)
values ('08e7882c-7596-43d1-b4cc-69f855210d72', 'a', 'b', 'c') returning *)
insert into endUsers (uuid, latestRevision)
values ('08e7882c-7596-43d1-b4cc-69f855210d72', revision.id);
这两种变体都失败了
专栏" lastid"不存在
或
缺少FROM-clause条目表#34;最后"
答案 0 :(得分:0)
失败的原因是因为每个子查询都可以作为表访问周围的上下文,而不是普通值。换句话说,必须使用如下的select语句访问它:
with revision as (
insert into endUserRevisions (endUser, name, company, email)
values ('08e7882c-7596-43d1-b4cc-79f855210d76', 'a', 'b', 'c') returning id)
insert into endUsers (uuid, latestRevision)
values ('08e7882c-7596-43d1-b4cc-79f855210d76', (select id from revision));
-- or
with revision as (
insert into endUserRevisions (endUser, name, company, email)
values ('08e7882c-7596-43d1-b4cc-79f855210d74', 'a', 'b', 'c') returning id)
insert into endUsers (uuid, latestRevision)
select '08e7882c-7596-43d1-b4cc-79f855210d74', revision.id from revision;