adonisjs / lucid

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

Preload nullable foreign key isnt working when null #1032

Closed q8tywolf closed 4 months ago

q8tywolf commented 4 months ago

Package version

20.6.0

Describe the bug

I have a user model in adonis and Job model

In the job model I have the following

Migration note this foreign key is nullable ... table.integer('handled_by').nullable().unsigned() .references('id').inTable('users') ...

Model ... @belongsTo(() => User, { localKey: 'handledBy', foreignKey: 'id' }) declare handlingBy: BelongsTo<typeof User>

@column({ columnName: "handled_by" }) declare handledBy: number; ... Query

await Job.query().where('listedBy', user.id).preload('listingBy').preload('handlingBy')

Error When the handledBy is null, I get the error "status_message": "\"Job.handlingBy\" expects \"handledBy\" to exist on \"User\" model, but is missing",

Reproduction repo

No response

q8tywolf commented 4 months ago

Fixed I had to make the localKey this key in User with BelongsTo