Skip to content

Service.remove stops working with custom createQuery method #3582

@BenPortner

Description

@BenPortner

When joining tables in a custom createQuery method, the remove method of that service stops working.

Tested with feathers 5.0.33 and SQLite database.

Example:
I have three services: users describes users, rooms describes rooms and roomjoins maps users to rooms (which user is in which room?). Each room further has a presenter. I want to be able to query the presenterId, which is why I have this custom createQuery method in the roomjoins service:

  createQuery(params: ServiceParams) {
    const query = super.createQuery(params)
    query
      .join('rooms as room', 'roomjoins.roomId', 'room.id')
      .select('room.presenterId as room_presenterId')

Now, the create and find methods work just fine with a room_presenterId query. But the remove method errors out. Digging deeper, I found out that find uses KnexAdapter._find, which in turn uses my createQuery method to build the correct query (line 170). remove on the other hand uses KnexAdapter._delete, which in turn uses KnexAdapter.knexify (line 349), which produces the wrong query:

# builder in KnexAdapter._find
select `roomjoins`.*, `room`.`presenterId` as `room_presenterId` from `roomjoins` inner join `rooms` as `room` on `roomjoins`.`roomId` = `room`.`id` where `roomId` = 446 and `playerId` = 731 and `room_presenterId` = 732
# q in KnexAdapter._remove
select * from `roomjoins` where `roomId` = 446 and `playerId` = 731 and `room_presenterId` = 732 and `id` in (118)

KnexAdapter.knexify removes all join statements, which is correct for SQLite databases. However, it still needs to add the room_presenterId information somehow. One valid query would be:

delete from `roomjoins` where `roomId` = 446 and `playerId` = 731 and EXISTS (
    SELECT 1 FROM rooms AS room
    WHERE room.id = roomjoins.roomId
      AND room.presenterId = 732
  );

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