从Sequelize中的交叉引用表获取值时出错-错误422

时间:2019-06-02 07:08:15

标签: include sequelize.js mysql-workbench cross-reference http-status-code-422

  

我正在尝试使用Sequelize从关联表中获取数据

     
    
        
  1. 父表[主表]
  2.     
  3. 学校表[保存所有学校信息]
  4.     
  5. ParentSchool表[交叉引用表]
  6.     
  

我正在尝试通过选择学校名称来过滤数据,作为回报,我需要json结果用于续集FINDALL以返回与该学校关联的所有父母。

                {
                    //includes the cross-reference table to join the schools with parents 
                    include: [
                        { model: db.parentSchools }, 
                        {
                            model: db.schools, as: "schools",
                            where: {
                                [Op.eq]: [{ name: req.params.school }]
                            }
                        }
                    ]
                }
            ).then(function (results) {
                res.json(results);
                console.log("parents for a school ", results);
            })
排序关联语法
    Parent.belongsToMany(models.schools, {
      as: "schools",
      through: "parentSchools",
      foreignKey: "parentId"
    });
  };
两种协会
        // a school belongs to many parents
        School.belongsToMany(models.parents, {
            as: "parents",
            // cross-reference table because a parent can have many schools as well
            through: "parentSchools",
            foreignKey: "schoolId"
        });

返回一个json对象,其中包含输入的学校名称的值。

0 个答案:

没有答案