Skip to content

Duplicate key column in selection #1779

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

Closed
mivanus opened this issue Apr 24, 2024 · 6 comments
Closed

Duplicate key column in selection #1779

mivanus opened this issue Apr 24, 2024 · 6 comments
Assignees

Comments

@mivanus
Copy link

mivanus commented Apr 24, 2024

In some cases select statement contains duplicate key columns which causes BadSqlGrammarException when "order by" gets included in sql.

Code that causes duplication:

for (SqlIdentifier keyColumn : keyColumns) {
columnExpressions.add(table.column(keyColumn).as(keyColumn));
}

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 24, 2024
@schauder
Copy link
Contributor

Please provide a Minimimal Reproducable Example, preferable as a Github repository. Make sure to include the database, either as an in memory database or if that is not possible using Testcontainers.

@schauder schauder added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 25, 2024
@mivanus
Copy link
Author

mivanus commented Apr 25, 2024

I couldn't reproduce issue with H2 so i had to use Testcontainers+db2.

https://github.com/mivanus/spring-data-retional-issue-1779

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 25, 2024
@schauder
Copy link
Contributor

schauder commented May 6, 2024

This might be related to #1680

I was able to reproduce this with DB2.
Other databases don't throw an exceptions but still produce a select statement containing the same column twice in the select list.

For example:

SELECT 
  "BBB"."ID" AS "ID", 
  "BBB"."NAME" AS "NAME", 
  "BBB"."AAA_ID" AS "AAA_ID", 
  "ccc"."ID" AS "ccc_ID", 
  "ccc"."NAME" AS "ccc_NAME", 
  "ccc"."BBB_ID" AS "ccc_BBB_ID", 
  "BBB"."ID" AS "ID" 
FROM "BBB" LEFT OUTER JOIN "CCC" "ccc" ON "ccc"."BBB_ID" = "BBB"."ID" WHERE "BBB"."AAA_ID" = ? ORDER BY "ID"

@schauder
Copy link
Contributor

schauder commented May 6, 2024

After some more investigation, there are a couple of things wrong with the mapping.

The relation between Aaa and Bbb is one internal to the aggregate of Aaa, but you use also AggregateTemplate.insert(bbb) which only works for aggregate roots.
This does not work.
See https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates

Also if Bbb is part of the Aaa aggregate, it needs a proper key-column holding the index of the Aaa.bbbs list.
This is currently set to the "ID" which is the primary key of Bbb.
Again, this does not work.

If you have further questions about fixing the mapping I recommend asking on SO. With the spring-data-jdbc tag, I will see and probably answer it.

If you think there is still an error with the SQL generation, please provide an updated reproducer.
Otherwise this issue will be closed soon.

@schauder schauder added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels May 6, 2024
@schauder schauder self-assigned this May 6, 2024
@spring-projects-issues
Copy link

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label May 13, 2024
@spring-projects-issues
Copy link

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2024
@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants