While working with ObjectionJS on a project, I encountered a case where the bindings in the generated query were inverted.
Dependencies versions:
- objection 3.1.5
- knex 3.1.0
This bug seems to only occur
- on a
delete query
- using a modifier
- inside a
joinRelated
- that adds a binding to the SQL query
I've prepared a full reprodution project in this repository, it contains the database schema, the models and the code. Here is a quick overview:
- Three models (
Person, Tag and a relation PersonTag that links them)
- The
Person model has a favorite attribute and a category attribute
Person PersonTag Tag
________ <- _________ -> ____
id personId id
name tagId tag
category
favorite
I want to delete all the links (PersonTag) for favorite persons having a specific category. For reasons outside the scope of this issue, I use a modifier on my joinRelated clause instead of a where clause. I start by selecting the objects to be deleted
await PersonTagModel.query()
.joinRelated("person(favoriteFilter)")
.modifiers({
favoriteFilter: favoritePersonModifier
})
.where("person.category", "Follower")
.debug()
And the resulting query is correct:
- sql:
'select `personTag`.* from `personTag` inner join (select `person`.* from `person` where `favorite` = ?) as `person` on `person`.`id` = `personTag`.`personId` where `person`.`category` = ?'
- bindings:
[ 1, 'Follower' ]
Then I change this to a delete query:
await PersonTagModel.query()
.joinRelated("person(favoriteFilter)")
.modifiers({
favoriteFilter: favoritePersonModifier
})
.where("person.category", "Follower")
.delete()
.debug()
And the bindings are inverted:
- sql:
'delete `personTag` from `personTag` inner join (select `person`.* from `person` where `favorite` = ?) as `person` on `person`.`id` = `personTag`.`personId` where `person`.`category` = ?'
- bindings:
[ 'Follower', 1 ]
While working with ObjectionJS on a project, I encountered a case where the bindings in the generated query were inverted.
Dependencies versions:
This bug seems to only occur
deletequeryjoinRelatedI've prepared a full reprodution project in this repository, it contains the database schema, the models and the code. Here is a quick overview:
Person,Tagand a relationPersonTagthat links them)Personmodel has afavoriteattribute and acategoryattributeI want to delete all the links (
PersonTag) for favorite persons having a specific category. For reasons outside the scope of this issue, I use amodifieron myjoinRelatedclause instead of awhereclause. I start by selecting the objects to be deletedAnd the resulting query is correct:
'select `personTag`.* from `personTag` inner join (select `person`.* from `person` where `favorite` = ?) as `person` on `person`.`id` = `personTag`.`personId` where `person`.`category` = ?'[ 1, 'Follower' ]Then I change this to a
deletequery:And the bindings are inverted:
'delete `personTag` from `personTag` inner join (select `person`.* from `person` where `favorite` = ?) as `person` on `person`.`id` = `personTag`.`personId` where `person`.`category` = ?'[ 'Follower', 1 ]