简体   繁体   English

如何排序代码以连接两个以上的表?

[英]How to sequlieze code to join more than two tables?

I have several tables which I want to retrieve data from using sequelize. 我有几个表,我想通过使用sequelize来检索数据。 The schema for these tables appears below: 这些表的架构如下所示:

几个表的架构,包括“类别”,“子类别”,“用户技能”,“用户”和“用户”

I want to join three of these tables, but I'm getting error number 1054. 我想加入其中三个表,但出现错误号1054。

Here is my code: 这是我的代码:

Skill model: 技能模型:

    'use strict';
    module.exports=(sequelize,Datatypes)=>{
       const Skill=sequelize.define('userskills',{
      id:{type:Datatypes.INTEGER,autoincrement:true,primaryKey:true},
      Userid:{type:Datatypes.INTEGER,allowNull:false},
      Skill:{type:Datatypes.STRING,allowNull:false}
    });
       Skill.associate=(model)=>{
         Skill.belongsTo(model.User,{
           as:"User info",
           onDelete:"CASCADE",
           onUpdate:"CASCADE",
           foreignKey: 'Userid',
           targetKey:"id"
         });
         Skill.belongsTo(model.Subcategory,{
           as:"Skill info",
           onDelete:"CASCADE",
           onUpdate:"CASCADE",
           foreignKey:"Skillid",
           targetKey:"id"
         });
       };
       return Skill;
    };

The function called for getting the data : 该函数要求获取数据:

    router.get('/',(req,res)=>{
      model.Skill.findAll({
        include:[{all:true}]
      }).then(result=>{
        res.status(200).json({
          result
        });
      }).catch(err=>{
        res.status(500).json({
          error:err
        });
      });
    });

Your ER diagram doesn't appear to match the Sequelizer model objects... for example, is the FK in skills named "Skillid" (model) or "Skillkey" (ER)? 您的ER图似乎与Sequelizer模型对象不匹配……例如,技能名为“ Skillid”(模型)或“ Skillkey”(ER​​)的FK吗?

To trace sql error 1054 (unknown col): in the node.js log, find the query that Sequelizer has generated from your findAll, copy/paste to MySqlWorkbench and execute. 要跟踪SQL错误1054(未知col):在node.js日志中,找到Sequelizer从findAll生成的查询,将其复制/粘贴到MySqlWorkbench并执行。 Which column is wrong? 哪一列错了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM