JS Sequelize "Where Related" Query?
JS Sequelize "Where Related" Query?
我不知道如何执行特定类型的查询。
不确定Sequelize社区会怎么称呼它,但Codeigniter PHP框架将其称为"where_related"查询。
例如,假设我有两种对象类型:hotelRoom和roomStyle。
hotelRoom具有属性roomNumber和相关的roomStyle对象。
假设我想查找与具有roomNumber<200
Sequelize能在不使用原始SQL的情况下做到这一点吗?
查看文档中的急切加载和一对多关联。
var HotelRoom = sequelize.define('HotelRoom', { roomNumber: DataType.INTEGER })
, RoomStyle = sequelize.define('RoomStyle');
// this will add the HotelRoomId column to RoomStyle table
HotelRoom.hasMany(RoomStyle);
// create an instance of room and make it the parent of a style
HotelRoom.create({ roomNumber: 5 })
.then(function(room){
return RoomStyle.create()
.then(function(style){
room.addStyle(style)
})
})
然后,您可以使用热切加载方法返回一系列房间编号的所有房间样式。
RoomStyle.findAll({
include: [{
model: HotelRoom,
attributes: [],
where: { roomNumber: { lt: 200 } } // i.e. "less than 200"
}]
})
.then(function(res){
console.log(res)
});
所有这些都假设房间和样式之间的关系是一对多的。要定义多对多关系,只需定义返回到另一个方向的关系(以上代码仍然有效)。
// define a many-to-many relationship through the junction table "RoomsStyles"
HotelRoom.hasMany(RoomStyle, { through: RoomsStyles });
RoomStyle.hasMany(HotelRoom, { through: RoomsStyles });
干杯,希望这有帮助。
相关文章:
- 铬:“;未捕获的语法错误:意外的标记:"
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 使用“+="操作人员
- //而不是在src=“”上使用http://"属性
- "未捕获的语法错误:意外的标记}"
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- "实例范围”;TypeScript类的getter/setter
- Javascript复选框函数:;缺少:在属性id之后"
- "“;变量未引用正确的对象
- "日期“;AJAX请求返回的类型值未定义
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- "工具提示"jQuery插件坏了
- "锻造;React中的表达式
- 在javascript中接收来自mssql.query的变量
- 图像可以从源<img src=""/>.TEXT可以在没有javascript的情况下从外部
- Query JS Ext.data.Store
- 如何提取“;href"最近列表项中的属性值