我正在使用续集 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 => {
/* ... */
});
我知道,有人会在他们出生的那一年发帖是没有意义的,但这只是一个例子——随它去吧。 :)
我从这个文档中(大部分)发现了这一点:
http://docs.sequelizejs.com/en/latest/docs/models-usage/#eager-loading
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`;
与您发布的内容相比,上面的查询可能看起来有点复杂,但它所做的基本上只是对 users 表的所有列进行别名处理,以确保在返回时将它们放入正确的模型中并且不会与帖子模型混淆
除此之外,您会注意到它执行 JOIN 而不是从两个表中进行选择,但结果应该相同
进一步阅读:
http://docs.sequelizejs.com/en/latest/docs/associations/#one-to-one-associations
http://docs.sequelizejs.com/en/latest/docs/associations/#one-to-many-associations
http://docs.sequelizejs.com/en/latest/docs/models-usage/#eager-loading
SELECT * FROM posts JOIN users ON users.id = posts.user_id WHERE users.year_birth = 1984
User.hasMany(Post)
时,您将方法/属性添加到 User 对象,并且当您调用 Post.belongsTo(User)
时,您将方法添加到 Post 类。如果您总是从一个方向调用(例如 User.getPosts()),那么您不需要向 Post 对象添加任何内容。但是双方都有方法很好。
Model1.belongsTo(Model2, { as: 'alias' })
Model1.findAll({include: [{model: Model2 , as: 'alias' }]},{raw: true}).success(onSuccess).error(onError);
就我而言,我做了以下事情。在 UserMaster 中,userId 是 PK,在 UserAccess 中,userId 是 UserMaster 的 FK
UserAccess.belongsTo(UserMaster,{foreignKey: 'userId'});
UserMaster.hasMany(UserAccess,{foreignKey : 'userId'});
var userData = await UserMaster.findAll({include: [UserAccess]});
不定期副业成功案例分享
posts.user_id = users.id
?谢谢