简体   繁体   中英

How to sequlieze code to join more than two tables?

I have several tables which I want to retrieve data from using sequelize. The schema for these tables appears below:

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

I want to join three of these tables, but I'm getting error number 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)?

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. Which column is wrong?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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