-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
Model delete while joining other tables generates wrong query #1015
Comments
Anything? |
The issue seems to be coming directly from Knex (I suppose), because they have recently added this Can you please check the same query with Knex directly and then check if the issue persists? |
okay, will check and come back with the result |
@thetutlage issue is coming from the Knex knex/knex#2746. const db = knex.knex(Object.assign({}, getConfig(), { debug: false }))
var modelType = 'users'
var modelId = 1
var permissionSlugs = ['delete']
const r = await db.from('model_permissions')
.leftJoin('permissions as p', 'p.id', '=', 'model_permissions.permission_id')
.join('model_roles as mr', (joinQuery) => {
joinQuery.onVal('mr.model_type', modelType).andOnVal('mr.model_id', modelId)
// joinQuery.andOnVal('mr.model_id', modelId).andOn('mr.model_type', db.raw("'"+modelType+"'")) // solution
})
.where((subQuery) => {
subQuery
.where((query) => {
query
.where('model_permissions.model_type', modelType)
.where('model_permissions.model_id', modelId)
})
.orWhere((query) => {
query
.whereRaw('mr.role_id=model_permissions.model_id')
.where('model_permissions.model_type', 'roles')
})
})
.whereIn('p.slug', permissionSlugs)
.delete()
// .toSQL() this generates delete
from "model_permissions" using "permissions" as "p","model_roles" as "mr"
where (("model_permissions"."model_type" = $1 and "model_permissions"."model_id" = $2) or
(mr.role_id = model_permissions.model_id and "model_permissions"."model_type" = $3))
and "p"."slug" in ($4)
and "p"."id" = "model_permissions"."permission_id"
and "mr"."model_type" = "users" -- error here
and "mr"."model_id" = 1;
Error - to fix I used
|
Package version
^20.2.0
Describe the bug
DB: postgres
this is how my ORM query looks like
// then
this is what sql query is generated
bindings: [ 'users', 1, 'roles', 'edit' ],
error
- column "users" does not exist
correct query I gues should be something like this
bindings: [ 'users', 1, 'roles', 'edit' , 'users', 1],
debugger result
Reproduction repo
No response
The text was updated successfully, but these errors were encountered: