0

我是 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, 1, 'b2');

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

当我选择 ta an 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,
                "a_id": 1,
                "b_desc": "b2"
            }
        ]
    }
]

它也适用于 tb 和 tc:

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

但我不知道如何使它从 ta 到 tc 工作,有点结合两个查询。

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

提前感谢您对此的任何帮助。

PS:使用 Potstgres 12 和 postgREST 7

4

1 回答 1

1

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

GET localhost:3000/ta?select=*,tb(*,tc(*))
于 2020-08-28T17:05:08.347 回答