首页 \ 问答 \ 如何在nodejs中使用sequelize进行连接查询(How to make join queries using Sequelize on Node.js)

如何在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 MANUAL position. 答 : 应在使用手动操作的开关上使用选择开关在手动位置。
  • 我实际上发现这是一个非常有趣的问题,特别是考虑到可以使用Android中的复选框轻松模拟开关的功能。 根据Android开发者指南,一个复选框只是一种类型的开关。 检查下面的引号或阅读完整的描述。 复选框:复选框允许用户从一个集合中选择多个选项 。 避免使用单个复选框来关闭或打开选项。 而是使用开/关开关。 开/关开关:开/关开关切换单个设置选项的状态。 I actually find this to be quite an interesting question, particularly consid ...
  • 以下是此代码的行为方式。 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 / 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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)