Custom Create Many method not saving relations

Hi, I am trying to bulk insert with api endpoint and I created custom createMany method for this.

Operator Station Stat table has relations. When I send data like that place and operator ids are not saved. (other fields are saved)

(Place and Operator have those ids). Do you have any idea about that ? Note: When I send request to single create method with using this payload, place and operators are saved.
image

1 Like

Any solution to this?

How to solve this problem?

To avoid performance issues, bulk operations are not allowed on relations.

Well, at least it should be documented…

Is there any alternative solution for the same?

@Strapi please at least keep a way of bypassing that “limit”. like you can accept a flag to save relations on createMany, I don’t see how’s that impossible, you can simply console.warn the message so developer can make the tradeoff if needed.

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.
    },
})