adonisjs / lucid

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

Many-to-many preload not working through query #880

Closed waffleau closed 2 years ago

waffleau commented 2 years ago

Package version

Node.js and npm version

Sample Code (to reproduce the issue)

Preload is not working through a many-to-many relationship. I'm using Postgres and haven't tested any other databases.

I've defined two simple models joined through a pivot table (using bigInteger for column type):

@manyToMany(() => Category, { pivotTable: 'topics_categories' })
public categories: ManyToMany<typeof Category>

If I try to load categories through Topic.query().preload('categories') the returned category lists are always empty. If I retrieve a single topic and use topic.load('categories') they are loaded as expected.

Bug is reproduced in a test case in the linked repo. Also included a test case to show that it works with load. To reproduce just pull down the repo, set up .env and run node ace test.

BONUS (a sample repo to reproduce the issue)

https://github.com/waffleau/adonis-lucid-preload-bug

thetutlage commented 2 years ago

Hello 👋

Usually, the issue lies with the data type mis-match in the database. As I can see the id column in categories and the topics table is an integer and the FK inside the pivot table are bigInt https://github.com/waffleau/adonis-lucid-preload-bug/blob/master/database/migrations/1663380305927_topics_categories.ts#L9-L10

waffleau commented 2 years ago

Thanks @thetutlage, can confirm this was indeed the issue 🙈. Preload is working as expected now in both my toy repo and my main project. Appreciate your help!

shiny commented 7 months ago

Hello 👋

Usually, the issue lies with the data type mis-match in the database. As I can see the id column in categories and the topics table is an integer and the FK inside the pivot table are bigInt https://github.com/waffleau/adonis-lucid-preload-bug/blob/master/database/migrations/1663380305927_topics_categories.ts#L9-L10

But why not throw an error, even without a warning? I have been debugging whole night, finally find the answer here. 😅