Open holoyan opened 8 months ago
Anything?
The issue seems to be coming directly from Knex (I suppose), because they have recently added this onVal
method. https://github.com/knex/knex/pull/2746.
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 https://github.com/knex/knex/pull/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 - - column "users" does not exist
to fix I used raw
method
// replace
joinQuery.onVal('mr.model_type', modelType).andOnVal('mr.model_id', modelId)
// by
joinQuery.andOnVal('mr.model_id', modelId).andOn('mr.model_type', db.raw("'"+modelType+"'")) // solution
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