How to reload a Model after createMany?

Hi,

I need to get the automatically generated UUID when multiple users was created, because I need you to get your UUID before saving, but I’m having the following error:

TypeError: users.reload is not a function

Exemple:

User migration

table
  .uuid('uuid')
  .defaultTo(this.db.raw('uuid_generate_v4()')) // auto generate uuid
  .notNullable()
  .unique()

UserPost

const usersData = [
  { name: 'Foo1', password: 'Bar1' },
  { name: 'Foo2', password: 'Bar2' }
]

const users = await User.createMany(usersData)

await users.reload() // to get auto generated uuid, but getting error
1 Like

I don’t know if it is possible to load all relations of array by one query.
But u can do like this
for (const user of users.rows ) { await user.reload() }

2 Likes

I used this and it worked, thx!

for (const user of users) { await user.reload() }
const usersJSON = users.map(user=> user.$attributes)
1 Like

btw, adonis have its own method to convert database output to json.
just try

users.toJSON() 

instead of users.map()

1 Like
TypeError: users.toJSON is not a function

I think that function works only on getting from database, so in this case, .map will resolve, thx.