简体   繁体   中英

Aggregate error when trying to use many to many table with sequelize

I'm getting this error:

AggregateError: aggregate error
    at Promise.try.then
    ...

when trying to setup a many to many table in sequelize with this code:

let _document
db.document.create(payload, { include: ['documentAttributes', 'documentChildren'] })
  .then(document => {
    _document = document
    const promises = []

    // Add tags if there are any
    if (req.body.tags) {
      req.body.tags.forEach(tag => {
        promises.push(db.tag.findOrCreate({ where: { key: tag.key } }))
      })
    }

    return Promise.all(promises)
  })
  .then(tags => {
    if (tags.length > 0) {
      const allTags = tags.map(tag => tag[0])
      return _document.setTags(allTags) // THIS LINE CAUSES THE ISSUE
    }

document.js model:

'use strict'

module.exports = (sequelize, DataTypes) => {
  const document = sequelize.define('document', {
    id: {
      allowNull: false,
      primaryKey: true,
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4
    },
    ...
    lastUpdatedBy: {
      allowNull: false,
      type: DataTypes.UUID
    }
  },
  {
    updatedAt: 'lastUpdatedAt'
  })
  document.associate = function (models) {
    document.hasMany(models.documentAttribute)
    document.hasMany(models.documentChildren)
    document.belongsToMany(models.tag, { through: 'documentTags' })
  }
  return document
}

tag.js model:

'use strict'
module.exports = (sequelize, DataTypes) => {
  const tag = sequelize.define('tag', {
    id: {
      allowNull: false,
      primaryKey: true,
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4
    },
    key: {
      allowNull: false,
      type: DataTypes.STRING
    },
    value: {
      allowNull: false,
      type: DataTypes.STRING
    },
    lastUpdatedBy: {
      allowNull: false,
      type: DataTypes.UUID
    }
  },
  {
    updatedAt: 'lastUpdatedAt'
  })
  tag.associate = function (models) {
    tag.belongsToMany(models.document, { through: 'documentTags' })
  }
  return tag
}

documenttags.js model:

'use strict'
module.exports = (sequelize, DataTypes) => {
  const documentTags = sequelize.define('documentTags', {
    id: {
      allowNull: false,
      primaryKey: true,
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4
    },
    documentId: {
      allowNull: false,
      type: DataTypes.UUID
    },
    tagId: {
      allowNull: false,
      type: DataTypes.UUID
    },
    lastUpdatedBy: {
      allowNull: false,
      type: DataTypes.UUID
    }
  },
  {
    updatedAt: 'lastUpdatedAt'
  })
  documentTags.associate = function (models) {
    // associations can be defined here
  }
  return documentTags
}

After reviewing the docs and discussing in the chat. The through property is necessary to set extra properties on the many-to-many table. http://docs.sequelizejs.com/class/lib/associations/belongs-to-many.js~BelongsToMany.html

return _document.setTags(allTags, { through: { lastUpdatedBy: tag.lastUpdatedBy }})

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