spring-data-relational icon indicating copy to clipboard operation
spring-data-relational copied to clipboard

Invalid DELETE created in derived query

Open chanhyeong opened this issue 4 months ago • 4 comments

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.

  • 3.5.x: https://github.com/chanhyeong/spring-data-relational/commit/d94a1a93c033cb8f12caa1d4ecbd93e02ab013db
  • 4.0.0-M: https://github.com/chanhyeong/spring-data-relational/commit/e28a25018bbedb85804a9a39d076145e4f54dddb

If I misunderstood the operation, please let me know.

chanhyeong avatar Aug 19 '25 02:08 chanhyeong

Hi @chanhyeong, I’m interested in contributing to this issue!

Huiyeongkim avatar Aug 26 '25 13:08 Huiyeongkim

You are welcome to. Let me know if you need help.

schauder avatar Aug 26 '25 16:08 schauder

@Huiyeongkim I've been waiting a comment from maintainers, but it's okay if you want to do it!

chanhyeong avatar Aug 27 '25 00:08 chanhyeong

@chanhyeong Thank you for giving me the opportunity! I really appreciate it.

Huiyeongkim avatar Aug 29 '25 09:08 Huiyeongkim