Skip to content
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

Delete with Join #506

Open
leogdion opened this issue May 13, 2022 · 3 comments
Open

Delete with Join #506

leogdion opened this issue May 13, 2022 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@leogdion
Copy link

Describe the bug

Anytime a join or with are involved in a delete query, I receive the follow error:

[ WARNING ] server: missing FROM-clause entry for table "WorkoutGroup" (errorMissingRTE) [request-id: 7937469E-9905-4490-9C42-82712CBDFE6F]

Here's an example of query which fails:

    let items = WorkoutSchedule.query(on: request.db)
      .join(parent: \.$workoutGroup)
      .filter(\.$id == scheduleID)
      .filter(WorkoutGroup.self, \WorkoutGroup.$id == workoutGroupId)
      .filter(WorkoutGroup.self, \WorkoutGroup.$instructor.$id == userID)
    
    return items.delete().transform(to: HTTPResponseStatus.ok)

Here's the workaround

    let items = WorkoutSchedule.query(on: request.db)
      .join(parent: \.$workoutGroup)
      .filter(\.$id == scheduleID)
      .filter(WorkoutGroup.self, \WorkoutGroup.$id == workoutGroupId)
      .filter(WorkoutGroup.self, \WorkoutGroup.$instructor.$id == userID)         
    
    return items.all().flatMapEach(on: request.eventLoop, {$0.delete(on: request.db)}).transform(to: HTTPResponseStatus.ok)

In both cases WorkoutGroup is a parent of WorkoutSchedule.

To Reproduce

Packages in use:

    .package(url: "https://github.com/vapor/vapor.git", from: "4.5.0"),
    .package(url: "https://github.com/vapor/jwt.git", from: "4.0.0"),
    .package(url: "https://github.com/vapor/fluent.git", from: "4.0.0"),
    .package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.0.0"),

Expected behavior

Shouldn't need to query all to delete

Environment

  • Vapor Framework version: 4.5.0
  • Vapor Toolbox version: NA
  • OS version: macOS 12.3.1 (21E258)
@leogdion leogdion added the bug Something isn't working label May 13, 2022
@0xTim
Copy link
Member

0xTim commented May 15, 2022

@leogdion could you enable debug logging and add the generated SQL?

cc @gwynne

@gwynne
Copy link
Member

gwynne commented May 15, 2022

DELETE queries which require joins (also known as multi-table delete) are not currently supported by Fluent (or even by SQLKit), due to the lack of consistent support or syntax between different databases. Reclassifying as a high-priority feature request.

@gwynne gwynne added enhancement New feature or request help wanted Extra attention is needed and removed bug Something isn't working labels May 15, 2022
@samdze
Copy link

samdze commented Sep 22, 2022

Experienced the same issue.
Probably reported in the wrong place but here it is: vapor/fluent#748
I added logs and examples of the corresponding SELECT query in the OP.

Feel free to close it if not useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants