Seeding relationships elegantly #4636
Replies: 1 comment
-
For any passers-by, my transaction issue was due to a wrong usage of the Factories. I created a model instead of returning arguments, which messes up the transactions. This code is wrong import factory from '@adonisjs/lucid/factories'
import User from '#models/user'
export const UserFactory = factory
.define(User, async ({ faker }) => {
return User.create({
fullName: faker.person.fullName(),
email: faker.internet.email(),
password: 'password',
})
})
.build() but these two are ok and working fine export const UserFactory = factory
.define(User, async ({ faker }) => ({
fullName: faker.person.fullName(),
email: faker.internet.email(),
password: 'password',
}))
.build()
export const UserFactory = factory
.define(User, () => { })
.newUp((attributes) => {
return User.create({
fullName: faker.person.fullName(),
email: faker.internet.email(),
password: 'password',
})
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi guys
I'm trying to write a simple seeder for a common Organization > Team > User pattern and I wonder what is the more elegant way to handle relationships.
For most cases I don't want to include relationships into factories as it would make factories too tied to the seeding logic and kind of pointless imo
But it looks tricky to define relationships after having created the model because of transactions.
This would be the best to me but it does not work
is getting
So I refetch the model everytime, but it looks odd
Do you have any opinion about this ?
Beta Was this translation helpful? Give feedback.
All reactions