gpt4 book ai didi

node.js - Sequelize.js - "where"子句中连接表中的列

转载 作者:行者123 更新时间:2023-12-03 22:24:15 25 4
gpt4 key购买 nike

我在 Node.js 中有一个使用 Sequelize 作为 ORM 的项目。

我创建了一个多对多关系,我需要使用“where”子句中连接表中的一些列进行查询。

我有以下型号:

sequelize.define("User", {
//some values
}, {
"classMethods" : {
"associate" : function (models) {
User.hasMany(models.Book, { "through" : models.BorrowedBook });
}
}
});

sequelize.define("Book", {
//some values
}, {
"classMethods" : {
"associate" : function (models) {
Book.hasMany(models.User, { "through" : models.BorrowedBook });
}
}
});

sequelize.define("BorrowedBook", {
//some other values
"returnedAt" : DataTypes.DATE
}
);

如何在“where”子句中使用“BorrowedBook”中的一列?

例如。我想让所有用户都急切地加载至少有 1 本书并且“BorrowedBook.returnedAt”为空的书籍。

有没有办法,像这样?
db.Book
.find({
"include" : [
db.User,
{ "model" : db.BorrowedBook, "where" : { "borrowedAt" : null } }
]
})

最佳答案

您不能像这样包含直通模型,除非您实际直接设置了直通模型的关系,但您仍然无法完成您想要的。

最新的 Sequelize 大师应该支持以下内容:

db.Book.find({
include: [
{model: db.User, through: {
where: {borrowedAt: null}
}
]
})

关于node.js - Sequelize.js - "where"子句中连接表中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23652199/

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