我在这段代码上有3个表,表格是:
用户 - 提供有关用户的信息 小部件 - 有关小部件的信息 user_widgets - 用于前几个表之间的m-n关系
除了其他2个表的ID之外,最后一个表有2个额外字段:读取和写入
我用来连接这些表的代码是
Widget.belongsToMany(User, { through:User_widget, foreignKey: 'widget_id' });
User.belongsToMany(Widget, { through:User_widget, foreignKey: 'user_id' });
User_widget的别名是" perms",所以当我打电话
User.findOne ... then(function(user){
user.getWidgets().then(function(widgets) {
在返回的小部件对象中,我有perms对象,读取和写入2个字段,所以一切正常。
我的问题是我需要过滤小部件,这样我才能获得我读过的小部件。
我已经尝试了
user.getWidgets({
where : {
read : false
}})
但它产生一个像
这样的SQL查询SELECT "widget"."id", "widget"."name", "perms"."widget_id" AS "perms.widget_id", "perms"."user_id" AS "perms.user_id", "perms"."read" AS "perms.read", "perms"."write" AS "perms.write"
FROM "widgets" AS "widget"
WHERE "widget"."read" = false
INNER JOIN "user_widget" AS "perms" ON "widget"."id" = "perms"."widget_id" AND "perms"."user_id" = '5';
我需要这样的查询:
SELECT "widget"."id", "widget"."name", "perms"."widget_id" AS "perms.widget_id", "perms"."user_id" AS "perms.user_id", "perms"."read" AS "perms.read", "perms"."write" AS "perms.write"
FROM "widgets" AS "widget"
INNER JOIN "user_widget" AS "perms" ON "widget"."id" = "perms"."widget_id" AND "perms"."user_id" = '5' AND "perms"."read" = false;
答案 0 :(得分:0)
尝试:
user.getWidgets({
through: {
where : {
read : false
}
}
})