我正在使用sequelize ORM;一切都很棒而且干净,但是当我将它与
join
查询一起使用时遇到了问题。
我有两个模型:用户和帖子。
var User = db.seq.define('User',{
username: { type: db.Sequelize.STRING},
email: { type: db.Sequelize.STRING},
password: { type: db.Sequelize.STRING},
sex : { type: db.Sequelize.INTEGER},
day_birth: { type: db.Sequelize.INTEGER},
month_birth: { type: db.Sequelize.INTEGER},
year_birth: { type: db.Sequelize.INTEGER}
});
User.sync().success(function(){
console.log("table created")
}).error(function(error){
console.log(err);
})
var Post = db.seq.define("Post",{
body: { type: db.Sequelize.TEXT },
user_id: { type: db.Sequelize.INTEGER},
likes: { type: db.Sequelize.INTEGER, defaultValue: 0 },
});
Post.sync().success(function(){
console.log("table created")
}).error(function(error){
console.log(err);
})
我想要一个查询,该查询以包含发布该帖子的用户信息的帖子进行响应。在原始查询中,我得到这个:
db.seq.query('SELECT * FROM posts, users WHERE posts.user_id = users.id ').success(function(rows){
res.json(rows);
});
我的问题是如何更改代码以使用 ORM 样式而不是 SQL 查询?
虽然接受的答案在技术上没有错误,但它没有回答原始问题,也没有回答评论中的后续问题,而这正是我来这里寻找的。 但我想通了,所以就这样吧。
如果您想查找所有具有用户的帖子(并且仅查找具有用户的帖子),则 SQL 将如下所示:
SELECT * FROM posts INNER JOIN users ON posts.user_id = users.id
这在语义上与 OP 的原始 SQL 相同:
SELECT * FROM posts, users WHERE posts.user_id = users.id
那么这就是你想要的:
Posts.findAll({
include: [{
model: User,
required: true
}]
}).then(posts => {
/* ... */
});
将 required 设置为 true 是生成内连接的关键。 如果您想要左外连接(您可以在其中获取所有帖子,无论是否有用户链接),请将 required 更改为 false,或者将其保留,因为这是默认值:
Posts.findAll({
include: [{
model: User,
// required: false
}]
}).then(posts => {
/* ... */
});
如果您想查找出生年份为 1984 年的用户的所有帖子,您需要:
Posts.findAll({
include: [{
model: User,
where: {year_birth: 1984}
}]
}).then(posts => {
/* ... */
});
请注意,一旦您添加了 where 子句,required 默认为 true。
如果您想要所有帖子,无论是否有用户附加,但如果有用户则只包含 1984 年出生的帖子,然后将必填字段添加回:
Posts.findAll({
include: [{
model: User,
where: {year_birth: 1984}
required: false,
}]
}).then(posts => {
/* ... */
});
如果您想要所有名称为“Sunshine”的帖子,并且仅当它属于 1984 年出生的用户时,您可以这样做:
Posts.findAll({
where: {name: "Sunshine"},
include: [{
model: User,
where: {year_birth: 1984}
}]
}).then(posts => {
/* ... */
});
如果您想要名称为“Sunshine”的所有帖子,并且仅当它属于与帖子上的 post_year 属性匹配的同一年出生的用户时,您可以这样做:
Posts.findAll({
where: {name: "Sunshine"},
include: [{
model: User,
where: ["year_birth = post_year"]
}]
}).then(posts => {
/* ... */
});
我知道,有人在他们出生的那一年发帖子是没有意义的,但这只是一个例子 - 就这样吧。 :)
我(大部分)从这个文档中发现了这一点:
User.hasMany(Post, {foreignKey: 'user_id'})
Post.belongsTo(User, {foreignKey: 'user_id'})
Post.find({ where: { ...}, include: [User]})
这会给你
SELECT
`posts`.*,
`users`.`username` AS `users.username`, `users`.`email` AS `users.email`,
`users`.`password` AS `users.password`, `users`.`sex` AS `users.sex`,
`users`.`day_birth` AS `users.day_birth`,
`users`.`month_birth` AS `users.month_birth`,
`users`.`year_birth` AS `users.year_birth`, `users`.`id` AS `users.id`,
`users`.`createdAt` AS `users.createdAt`,
`users`.`updatedAt` AS `users.updatedAt`
FROM `posts`
LEFT OUTER JOIN `users` AS `users` ON `users`.`id` = `posts`.`user_id`;
与您发布的内容相比,上面的查询可能看起来有点复杂,但它所做的基本上只是对用户表的所有列进行别名,以确保它们在返回时放置到正确的模型中,并且不会与帖子模型混淆
除此之外,您会注意到它执行 JOIN 而不是从两个表中进行选择,但结果应该是相同的
进一步阅读:
Model1.belongsTo(Model2, { as: 'alias' })
Model1.findAll({include: [{model: Model2 , as: 'alias' }]},{raw: true}).success(onSuccess).error(onError);
就我而言,我做了以下事情。在 UserMaster userId 中是 PK,在 UserAccess userId 中是 UserMaster
的 FKUserAccess.belongsTo(UserMaster,{foreignKey: 'userId'});
UserMaster.hasMany(UserAccess,{foreignKey : 'userId'});
var userData = await UserMaster.findAll({include: [UserAccess]});
在模型文件中创建关联,然后使用 include 进行连接,您也可以使用内部包含来连接表。
----model.js
blog1.hasMany(blog2, {foreignKey: 'blog_id'})
blog2.belongsTo(blog1, {foreignKey: 'blog_id'})
-----controller.js
blog2.find({ where: {blog_id:1}, include: [blog1]})