Skip to content

Bindings are inverted when using joinRelated with a modifier in a delete query #2799

@glejune

Description

@glejune

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 ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions