Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preload nullable foreign key isnt working when null #1032

Closed
q8tywolf opened this issue May 29, 2024 · 1 comment
Closed

Preload nullable foreign key isnt working when null #1032

q8tywolf opened this issue May 29, 2024 · 1 comment

Comments

@q8tywolf
Copy link

q8tywolf commented May 29, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant