Custom Create Many method not saving relations

For hasMany and belongsTo relationship. My workaround for this is by updating the other relation. For example let’s say you have two content-types:

  • Book
  • Label

To do bulk insert and associate these labels with a Book. you can use the list of Label ids returned by strapi.db.query( ... ).createMany(...).. createMany() will return {count: <number of entries>, ids: [ <array of ids>] } after an successful operation.

Then, perform an update operation to the Book content-type with the ids:

const labelsOperation = await strapi.db.query("api::label.label").createMany({
    data: [
             {slug: 'new', name: 'New'},
             {slug: 'warehouse', name: 'Warehouse'},
             . . .
       ]
    }),
  })
await strapi.entityService.update("api::book.book", *<book_id>*, {
    data: {
      labels: labelsOperation.ids // this will use the hasMany relationship in the Book content-type.
    },
})
1 Like