gpt4 book ai didi

javascript - 如何在 Bookshelf.js 中执行多连接查询?

转载 作者:行者123 更新时间:2023-11-28 10:50:23 24 4
gpt4 key购买 nike

基本上,我有两个表:User 和 user_connections

用户:

id | email

1 | name@domain.com

2 | name1@domain.com

用户连接:

user_id | key

1 | test1

1 | test2

2 | test3

基本上,我需要构造一个查询,根据给定的电子邮件,提取与该电子邮件 ID 关联的连接列表。

我已经定义了 User 并指向 User 表。我如何获得所需的结果?

<小时/>

编辑:这是所需的结果:

如果我 GET 的 id 为 1,那么我应该得到以下返回值(JSON 格式):

{
{
user_id: 1,
key: test1
},
{
user_id: 2,
key: test2
}
}

最佳答案

使用withRelated在您传递给 fetch 的哈希中。它标识您必须在书架模型中定义的列名称数组。

User
.forge()
.where({email: "me@me.com"})
.fetch({
withRelated: ["user_connections"] //or however you have defined it in your model defintion
})
.then(function(collection) {
collection.relations// {user_connections: [ {}, {}, ...]}
});

关于javascript - 如何在 Bookshelf.js 中执行多连接查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34171404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com