微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

嵌套的嵌入式资源

如何解决嵌套的嵌入式资源

我是postgREST的新手。我已经设置好了,并且可以在我的数据库上正常工作。我正在浏览文档,我认为我可以使用Resource Embedding,但是我不知道如何使它以嵌套方式工作。

我的架构具有类似于以下内容的表:

create table ta (
    a_id integer primary key,a_desc varchar(50)
);

create table tb (
    b_id integer primary key,a_id integer not null,b_desc varchar(50),constraint tb_fk1 foreign key (a_id) references ta(a_id)
);

create table tc (
    c_id integer primary key,b_id integer not null,c_desc varchar(50),constraint tc_fk1 foreign key (b_id) references tb(b_id)
);

insert into ta values (1,'a1');

insert into tb values (1,1,'b1');
insert into tb values (2,'b2');

insert into tc values (1,'c1');
insert into tc values (2,'c2');
insert into tc values (3,2,'c3');
insert into tc values (4,'c4');

当我选择一个tb时,资源嵌入起作用:

localhost:3000/ta?select=*,tb(*)

[
    {
        "a_id": 1,"a_desc": "a1","tb": [
            {
                "b_id": 1,"a_id": 1,"b_desc": "b1"
            },{
                "b_id": 2,"b_desc": "b2"
            }
        ]
    }
]

它还可以与tb和tc一起使用:

localhost:3000/tb?select=*,tc(*)
[
    {
        "b_id": 1,"b_desc": "b1","tc": [
            {
                "c_id": 1,"b_id": 1,"c_desc": "c1"
            },{
                "c_id": 2,"c_desc": "c2"
            }
        ]
    },{
        "b_id": 2,"b_desc": "b2","tc": [
            {
                "c_id": 3,"b_id": 2,"c_desc": "c3"
            },{
                "c_id": 4,"c_desc": "c4"
            }
        ]
    }
]

但是我不知道如何使它从ta到tc都能正常工作,将两种查询结合在一起。

有人知道我能做到这一点吗?最好使用查询字符串,但也可以使用视图或存储过程。

在此先感谢您的帮助。

PS:使用Potstgres 12和postgREST 7

解决方法

对于嵌套资源嵌入,您可以执行以下操作:

GET localhost:3000/ta?select=*,tb(*,tc(*))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。