Select from many-to-many relationship sequelize

自古美人都是妖i 提交于 2019-12-08 02:05:43

问题


I tried to select from a table with reference another table. I have a relationship many-to-many between table food and table Ingredients.

Food model:

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('food', {
    id: {
      type: DataTypes.INTEGER(10),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    name_food: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    tableName: 'food',
    freezeTableName: true
  });
};

Food_ingredients model:

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('food_ingredients', {
    id: {
      type: DataTypes.INTEGER(10),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    food_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false,
      references: {
        model: 'food',
        key: 'id'
      }
    },
    ingredient_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false,
      references: {
        model: 'ingredients',
        key: 'id'
      }
    }
  }, {
    tableName: 'food_ingredients',
    freezeTableName: true
  });
};

Ingredients model:

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('ingredients', {
    id: {
      type: DataTypes.INTEGER(10),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    name_ingredient: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    tableName: 'ingredients',
    freezeTableName: true,
    timestamps: false
  });
};

My problem is that I don't know how to make a natural join on this tables with sequelize. I tried something like this:

Food.findAll({include: [
    {
      model: Food_Ingredients
    }
  ]}).then(responseWithResult(res))
     .catch(handleError(res));

But I received this error:

food_incredients is not associated to food!

So, I how can I query that with sequelize?

Thanks.


回答1:


It doesn't seem like you defined the many-to-many association between food and ingredients. In summary, you need to add something like this to your models:

Food model:

Food.belongsToMany(Ingredients, { through: Food_ingredients});

Ingredients model:

Ingredients.belongsToMany(Food, { through: Food_ingredients});

Then, when you want to query, you don't include the "through" model, but the other model in the relation. In your case:

Food.findAll({include: [
{
  model: Ingredients
}]}).then(responseWithResult(res)).catch(handleError(res));

Sequelize will do the join for you. Note that if you give your relationship an alias, like:

Food.belongsToMany(Ingredients, {as 'someAlias', through: Food_ingredients});

You need to add that alias in your include:

Food.findAll({include: [
{
  model: Ingredients, as 'someAlias'
}]}).then(responseWithResult(res)).catch(handleError(res));


来源:https://stackoverflow.com/questions/34463180/select-from-many-to-many-relationship-sequelize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!