adonisjs / lucid

AdonisJS SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
https://lucid.adonisjs.com/
MIT License
1.02k stars 189 forks source link

Factory with BelongsTo relationship fails with `this.factory is not a function` #1000

Closed SerdarSanri closed 4 months ago

SerdarSanri commented 4 months ago

Package version

18.4.2

Describe the bug

While creating a seeder with factory, any relationship with BelongsTo throws an exception as this.factory is not a function. It appears to be thrown in Factory/Relations/Base.js:47 where it tries to run factory as a function yet, factory is an object. Please see the attached screenshot below.

Seeder image

PatientFactory image

UserFactory image

PatientModel image

Exception thrown at node_modules/@adonisjs/lucid/build/src/Factory/Relations/Base.js:47 ( https://github.com/adonisjs/lucid/blob/e038c67dd93b750f97240c2de98791e350c0f4bd/src/factories/relations/base.ts#L41 ) image

image

Reproduction repo

No response

Julien-R44 commented 4 months ago

Should be a callback

https://lucid.adonisjs.com/docs/model-factories#relationships

export const UserFactory = Factory.define(User, ({ faker }) => {
  return {
    username: faker.internet.userName(),
    email: faker.internet.email(),
    password: faker.internet.password(),
  }
})
  .relation('posts', () => PostFactory) // 👈
  .build()