spring-projects / spring-projects/spring-data-relational

Invalid DELETE created in derived query

Open
#2,123 4 comments 0 reactions 1 assignee View on GitHub

@schauder is already working on this.

Since Aug 19, 2025.

type: bug
Dominant language
Java
Stars
827
Forks
394
PR merge metrics
No merged PRs in 30d

Description

Issue

If child object has an id column which named different with parent's id,
an invalid DELETE query created and failed to execute the test JdbcRepositoryWithCollectionsChainHsqlIntegrationTests#deleteByName.
(in 3.5.x, 4.0.0-M versions)

I reproduced in commit https://github.com/chanhyeong/spring-data-relational/commit/7cca9ee7d7ca8a5c1ee0bd32d76164e93e30eb17, and it creates an error message below.

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [DELETE FROM "GRAND_CHILD_ELEMENT" WHERE "GRAND_CHILD_ELEMENT"."CHILD_ELEMENT" IN (SELECT "CHILD_ELEMENT"."ID" FROM "CHILD_ELEMENT" WHERE "CHILD_ELEMENT"."DUMMY_ENTITY" IN (SELECT "DUMMY_ENTITY"."ID" FROM "DUMMY_ENTITY" WHERE "DUMMY_ENTITY"."NAME" = ?))]

Here is the query in message to read easily.

DELETE FROM
  "GRAND_CHILD_ELEMENT"
WHERE
  "GRAND_CHILD_ELEMENT"."CHILD_ELEMENT" IN (
    SELECT
      "CHILD_ELEMENT"."ID"
    FROM
      "CHILD_ELEMENT"
    WHERE
      "CHILD_ELEMENT"."DUMMY_ENTITY" IN (
        SELECT
          "DUMMY_ENTITY"."ID"
        FROM
          "DUMMY_ENTITY"
        WHERE
          "DUMMY_ENTITY"."NAME" = ?
    )
  )

Expected

It should select "CHILD_ELEMENT"."CHILD_ID" in first IN statement, instead of "CHILD_ELEMENT"."ID".

Resolution

If JdbcDeleteQueryCreator#deleteRelation takes the id column of current path on SELECT phrase, this issue will be fixed.

If I misunderstood the operation, please let me know.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.