如何在nodejs中使用sequelize进行连接查询(How to make join queries using Sequelize on Node.js)
我正在使用序列化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查询?
I am using sequelize ORM; everything is great and clean, but I had a problem when I use it with
join
queries. I have two models: users and posts.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); })
I want a query that respond with a post with the info of user that made it. In the raw query, I get this:
db.seq.query('SELECT * FROM posts, users WHERE posts.user_id = users.id ').success(function(rows){ res.json(rows); });
My question is how can I change the code to use the ORM style instead of the SQL query?
原文:https://stackoverflow.com/questions/20460270
更新时间:2024-01-15 13:01
最满意答案
问 : Provision shall be made on the switch for a manual operation using the selector switch in the MANUAL position. 答 : 应在使用手动操作的开关上使用选择开关在手动位置。
其他回答
我。。知。。道 加。。我。。私。。聊
相关问答
更多-
Provision shall be made on the switch for a manual operation using the selector switch in the MA[2023-09-30]
问 : Provision shall be made on the switch for a manual operation using the selector switch in the MANUAL position. 答 : 应在使用手动操作的开关上使用选择开关在手动位置。 -
我实际上发现这是一个非常有趣的问题,特别是考虑到可以使用Android中的复选框轻松模拟开关的功能。 根据Android开发者指南,一个复选框只是一种类型的开关。 检查下面的引号或阅读完整的描述。 复选框:复选框允许用户从一个集合中选择多个选项 。 避免使用单个复选框来关闭或打开选项。 而是使用开/关开关。 开/关开关:开/关开关切换单个设置选项的状态。 I actually find this to be quite an interesting question, particularly consid ...
-
C ++ Switch Cases(C++ Switch Cases)[2023-08-23]
以下是此代码的行为方式。 x等于零 所以cout<<"Zero"; 被执行。 因为没有break; 在它之后, 执行第二种情况: cout<<"Hello World"; 而自从cout<<"something"; 打印后不添加换行符,它们打印为单个单词。 Here's how this code behaves. x is equal to zero so cout<<"Zero"; is executed. Since there's no break; after it, the second cas ... -
在我看来,你的教师不希望你开始使用switch语句,而只是使用if流控制结构: if(booleanExpression) { } else if(booleanExpression2) { } It would appear to me that your instructor does not want you to use the switch statement to begin with, but simply use the if flow control structure: if(bo ...
-
我认为这个消息 重新宣布'int choice' 应该很明显。 其他错误信息有点难以理解,但是如果在switch语句之外声明变量guess ,它将消失。 I think the message redeclaration of `int choice' should be pretty obvious. The other error messages are a little harder to understand, but will disappear if you declare the varia ...
-
一些语言也允许在switch语句中进行不等式比较,例如Visual Basic: Select Case x Case < 5 // ... Case 7 // ... Case 10 To 15 // ... Case Else // ... End Select 基本上,是的, switch通常可以写成if / else if梯形图,区别在于switch将一个值与多个比较值进行比较, if / else if可以在每个单独分支中执行任何操作。 但即使如此 ...
-
C ++ Switch语句(C++ Switch Statement)[2022-09-06]
原因是C / C ++ switch语句采用int参数而不支持string作为类型。 虽然它支持常量数组的概念。 还要提到的是,C / C ++ switch语句通常作为分支表生成。 并且使用string样式开关生成分支表并不容易。 在C ++中,switch语句将int作为参数。 为什么你不能在开关中串起来并得到以下错误? basic.cpp:42:28: warning: multi-character character constant [-Wmultichar] c ... -
UITabBar只显示一个标签栏,并在用户选择其他标签时告诉其委托。 UITabBarController完全符合您的要求:它管理视图控制器的集合,在用户选择不同的选项卡时在它们之间切换。 使用UITabBarController没有什么神奇或不可编程的 - 如果你不想使用故事板,你总是可以在代码中配置UITabBarController 。 除非你没有告诉我们,否则你应该使用UITabBarController 。 如果您真的只想使用UITabBar ,可以在视图控制器中实现UITabBarDelegat ...
-
首先是对您尝试的内容的分析: 1) 合并两棵不同的树木 如https://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-dug-merge.html所示 ,它适用于您的情况,如果: - 你有与myBranch链接的工作副本 - 然后在TortoiseSVN窗口中选择合并FROM myBranch to theirBranch(而不是相反,我想你做了:它解释了你观察到的冲突)。 但是,当没有可以使用的合并跟踪功能时,这种合并很有用,这可能不是您的情况。 2) ...
-
performOperation方法接受类型为(Double, Double) -> Double 。 现在这个论点可以是以下任何一个: 方法1。 关闭类型(Double, Double) -> Double 方法2。 签名为(Double, Double) -> Double方法名称 以下示例使用两种方法: func operation() { let operate = sender.currentTitle! switch operate ...