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 gett...

问题:

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

Schema for several tables including 'category', 'subcategory', 'userskills', 'users', and 'userads'

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
        });
      });
    });

回答1:

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?

  • 发表于 2019-04-01 02:16
  • 阅读 ( 172 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除