Complex where queries: Integrity constraint violation: 1052 Column "XXX" in where clause is ambiguous
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 3.5k
- Forks
- 468
- Avg merge
- 3h 9m
- Merged PRs (30d)
- 2
Description
Describe the bug
Querying a certain field on a relation of the resulting type of the query and applying a complex where query to filter the relation causes the SQL error:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'ID' in where clause is ambiguous
Expected behavior/Solution
I feel like selections in the query need to be prefixed, or addSelect should be used (if it isn't already)
Steps to reproduce
- Query some data and request a field on a relation of the data
- Have a
hasXcomplex where filter to filter the queried field, e.g. ID or name etc.
Output/Logs
Example error: (see example code below)
"debugMessage": "SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'ID' in where clause is ambiguous (SQL: select count(*) as aggregate from `teams` where exists (select * from `players` inner join `player_team` on `player_team`.`player_id` = `players`.`id` where `teams`.`id` = `player_team`.`team_id` and (`ID` = 120)))",
"message": "Internal server error",
"extensions": {
"category": "internal"
},
Lighthouse Version
"nuwave/lighthouse": "^4.12",
Example code:
Query:
(Requesting teams of players, request players of the teams (paginated) and filter the players relation for an ID)
query {
teams(
hasPlayers:
{
column: ID value:120
}
) {
data {
id
players {
data {
id
}
}
}
}
}
Example GraphQL query file:
extend type Query @guard(with: ["api"]) {
teams(
hasPlayers: _ @whereHasConditions(columnsEnum: PlayerColumn)
): [Team!]
@paginate
}
Team type:
type Team {
"The ID of this team"
id: ID!
"Players of this team"
players: [Player!] @hasMany(type: "paginator")
}
Player type:
type Player {
"The ID of this player"
id: ID!
}
Playercolumn example:
enum PlayerColumn {
ID
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the example GraphQL query in the issue, focusing on the teams-to-players relation and the hasPlayers filter using PlayerColumn.ID. Reproduce the generated SQL and trace the relation filtering path that builds it. Done means the equivalent nested query no longer raises the ambiguous-column error and existing relation filtering still works.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, laravel, php
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100