lookinlab / adonis-lucid-soft-deletes

Addon for soft deletes AdonisJS Lucid ORM
MIT License
93 stars 7 forks source link

Breaks where in related query #12

Closed atharahmed closed 2 years ago

atharahmed commented 2 years ago

Hi,

Recently I updated Adonis, Lucid, and soft-deletes to their latest versions, and now where query in preload is broken.

Following are the version updates:

@adonisjs/core ^5.5.3 → ^5.8.3 @adonisjs/lucid ^17.1.0 → ^18.0.0 adonis-lucid-soft-deletes ^1.3.3 → ^1.4.1

I get the "Cannot read properties of undefined (reading 'method')" error.

So I inspected to find the error and seems like the issue is caused by the toQuery() method on line 32 of the following file: https://github.com/lookinlab/adonis-lucid-soft-deletes/blob/develop/src/SoftDeletes/index.ts

Replacing const isGroupLimitQuery = query.toQuery().includes('adonis_group_limit_counter') with let sqlQuery = query.toSQL().toNative() const isGroupLimitQuery = sqlQuery.sql.includes('adonis_group_limit_counter'); seem to fix the issue.

I can create a PR if that helps.

Thanks

LookinGit commented 2 years ago

Hi @atharahmed Can you send callstack of error "Cannot read properties of undefined (reading 'method')"?

atharahmed commented 2 years ago

Please see attached image:

Screenshot 2022-05-28 at 10 42 27 PM

I'm trying to test the suggested fix a fork of the package.

LookinGit commented 2 years ago

I don't think what replace toQuery to toSQL will fix this issue. Both methods call applyWhere() where this error occurs

Need check why firstItem is undefined.

atharahmed commented 2 years ago

My suggested fix does not work but it seems like when you transform the lucid object to SQL query it adds an empty array in chainable of lucid which throws the error.

You can recreate the issue by replacing .preload('authors', (authors) => authors.groupLimit(2)) with .preload('authors', (authors) => authors.where('name', 'John')) in relations.spec.ts file line 226

LookinGit commented 2 years ago

Fixed in v1.4.2