lucid icon indicating copy to clipboard operation
lucid copied to clipboard

Custom data passed to createMany does not reach blueprint

Open chrisdc opened this issue 9 years ago • 1 comments

Hello, I have the following seed and factory:

const countriesArray = await Factory
  .model('App/Models/Country')
  .createMany(3, [
    {
      country: 'Afghanistan',
      code: 'AF'
    },
    {
      country: 'Aland Islands',
      code: 'AX'
    },
    {
      country: 'Albania',
      code: 'AL'
    }
  ])
Factory.blueprint('App/Models/Country', (faker, i, data) => {
  return {
    name: data.country,
    code: data.code
  }
})

When I run adonis seed the process fails due to the NOT NULL constraints on both columns - data.country and data.code are undefined.

I may have misunderstood, but it it possible that this line should read like this this?

return Promise.all(_.map(_.range(numberOfRows), (index) => this.create(data[index])))

chrisdc avatar Oct 24 '17 13:10 chrisdc

There are couple of issues here.

data is the array you passed to createMany method and not the object. But you can get the object by using the i (index)

Factory.blueprint('App/Models/Country', (faker, i, data) => {
 return {
   name: data[i].country,
   code: data[i].code
 }
})

Next, the i variable sent by Factory class is not incremented, which I have fixed in the related commit

thetutlage avatar Oct 24 '17 16:10 thetutlage