简体   繁体   中英

Sequelize belongsToMany get source model by querying on target model

I have two models Brand and Campaign .

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

User.findAll({ include: [{ model: Project, through: { attributes: ['createdAt', 'startedAt', 'finishedAt'], where: {completed: true} } }] });

I have found some ways to work around, but it is not what I am looking for:

SOLUTION 1:

Update belongsToMany Brand to hasMany CampaignBrand and the query by CampaignBrand.brand

SOLUTION 2:

Get Campaign by querying Brand

Any other advices?

Dialect: postgres

Database version: 9.4

Sequelize version: 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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