简体   繁体   English

通过查询目标模型来使belongsToMany获得源模型

[英]Sequelize belongsToMany get source model by querying on target model

I have two models Brand and Campaign . 我有两个模型BrandCampaign

A Brand can have many Campaigns 一个品牌可以有多个广告系列

export default(sequelize, DataTypes)=> {
  const Brand = sequelize.define('Brand', {
    id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
    },
 })

 Brand.associate = models=> {
    Brand.belongsToMany(models.Campaign, {
        through: models.CampaignBrand,
        foreignKey: 'brand',
    })
 }

 return Brand
}

A Campaign can also have many Brand 一个广告系列也可以有很多品牌

export default(sequelize, DataTypes)=> {
  const Campaign = sequelize.define('Campaign', {
    id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
    },
 })

 Campaign.associate = models=> {
    Campaign.belongsToMany(models.Brand, {
        through: models.CampaignBrand,
        foreignKey: 'campaign',
    })
 }

 return Campaign
}

And here is through model: 这是通过模型:

export default(sequelize, DataTypes)=> {
  const CampaignBrand = sequelize.define('CampaignBrand', {
    // see enums
    status: {
        type: DataTypes.INTEGER,
        allowNull: false,
    },
    roleText: {
        type: DataTypes.STRING,
    },
  })

  CampaignBrand.associate = models=> {
    CampaignBrand.belongsTo(models.Campaign, {
        foreignKey: 'campaign',
        targetKey: 'id',
        onDelete: 'CASCADE',
    })
  }

  return CampaignBrand
}

In case I want to get Campaigns by brand . 如果我想按品牌 获得Campaigns What should I do? 我该怎么办? I have tried query likes document mentioned but it does not work for me 我已经尝试过查询喜欢提到的文档,但是它对我不起作用

With Belongs-To-Many you can query based on through relation and select specific attributes. 使用“多对多”,您可以基于直通关系进行查询并选择特定的属性。 For example using findAll with through 例如,将findAll与through一起使用

User.findAll({ include: [{ model: Project, through: { attributes: ['createdAt', 'startedAt', 'finishedAt'], where: {completed: true} } }] }); User.findAll({包括:[{模型:项目,通过:{属性:['createdAt','startedAt','finishedAt'],其中:{completed:true}}}]}}));

I have found some ways to work around, but it is not what I am looking for: 我找到了一些解决方法,但这不是我想要的:

SOLUTION 1: 解决方案1:

Update belongsToMany Brand to hasMany CampaignBrand and the query by CampaignBrand.brand belongsToMany Brand更新为hasMany CampaignBrand,并通过CampaignBrand.brand查询

SOLUTION 2: 解决方案2:

Get Campaign by querying Brand 通过查询品牌获取广告系列

Any other advices? 还有其他建议吗?

Dialect: postgres 方言: postgres

Database version: 9.4 数据库版本: 9.4

Sequelize version: 4.2.1 续集版本: 4.2.1

I think you don't need this association in the the through model: 我认为您在直通模型中不需要此关联:

CampaignBrand.associate = models=> { CampaignBrand.belongsTo(models.Campaign, { foreignKey: 'campaign', targetKey: 'id', onDelete: 'CASCADE', }) }

You already have the belongsToMany association in the definitions of Brand and Campaign, so I think you just need to create the CampaignBrand model with your status and roleText attributes. 您已经在Brand和Campaign的定义中拥有了EmiratesToMany关联,因此我认为您只需要使用status和roleText属性创建CampaignBrand模型。

As I understand it, then you can query brands through campaigns and it should return each brand element and its associated campaigns, 据我了解,您可以通过广告系列查询品牌,并且它应该返回每个品牌元素及其关联的广告系列,

Brand.findAll({ include: [{ model: Campaign }] });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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