排序包含引发不相关的错误

时间:2019-04-21 18:45:09

标签: sequelize.js associations

我有一个Person表,成分表和一个PersonIngredient表。 personingredient通过person_ingredient相互关联。当我查询person_ingredient并尝试包含ingredient时,我得到"ingredient is not associated to person_ingredient!"

为什么后遗症不允许我加入此行?

人员协会:

person.associate = function(models) {
  // associations can be defined here
  person.belongsToMany(models.ingredient, {through: {model: models.person_ingredient, unique: false}});
};

成分协会:

ingredient.associate = function(models) {
  ingredient.belongsToMany(models.person, {through: {model: models.person_ingredient, unique: false}});
};

person_redredient定义:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const person_ingredient = sequelize.define('person_ingredient', {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true, 
      autoIncrement: true,
      allowNull: false
    },
    person_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      references: {
        model: 'person',
        key: 'id'
      },
      onUpdate: 'cascade',
      onDelete: 'cascade'
    },
    ingredient_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      references: {
        model: 'ingredient',
        key: 'id'
      },
      onUpdate: 'cascade',
      onDelete: 'cascade'
    },
    archived: {
      type: DataTypes.BOOLEAN,
      defaultValue: false
    },
    custom_ingredient: {
      type: DataTypes.TEXT
    }
  }, {
    underscored: true,
    freezeTableName: true
  });
  person_ingredient.associate = function(models) {
    // associations can be defined here
  };
  return person_ingredient;
};

查询:

let person = await db.Person.findOne({where:{email : email}});
let personIngredients = await db.PersonIngredient.findAll({
    include: [
        db.Ingredient
    ],
    where:{person_id: person.id}
});

应该导致

SELECT person_ingredient.*, ingredient.*
FROM person_ingredient
JOIN ingredient ON ingredient.id = person_ingredient.ingredient_id
WHERE person_ingredient.person_id = 'id'

0 个答案:

没有答案
相关问题