Skip to content

Bug: MySQL WHERE clause column qualification fails when column is on right side of comparison operator. #3494

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

Open
kmirzavaziri opened this issue Apr 18, 2025 · 2 comments · May be fixed by #3495
Labels
bug Something isn't working

Comments

@kmirzavaziri
Copy link

Bug description

MySQL WHERE clause column qualification fails when the column is on the right side of a comparison operator. This causes the query to be generated incorrectly as the table alias is not properly added to the column name.

For example, with the WHERE clause 100 > user_id, the generated query incorrectly shows:

WHERE 100 > user_id

Instead of the correct:

WHERE 100 > t_09a0eed1cbbe07ca.user_id

This only affects MySQL queries. PostgreSQL queries work correctly regardless of column position.

How to reproduce

  1. Use MySQL driver with a query having a WHERE clause
  2. Put the column name on the right side of comparison (e.g., 100 > user_id)
  3. Run the query builder
  4. Observe that the generated query has unqualified column name

Minimal reproduction test:

func Test_BuildQuery_MySQLColumnQualification(t *testing.T) {
    t.Run("mysql column on right", func(t *testing.T) {
        assert.Equal(t,
            "SELECT `orders`.`order_id`, `orders`.`user_id` FROM `public`.`orders` AS `orders` INNER JOIN `public`.`users` AS `t_09a0eed1cbbe07ca` ON (`t_09a0eed1cbbe07ca`.`user_id` = `orders`.`user_id`) WHERE 100 > t_09a0eed1cbbe07ca.user_id ORDER BY `orders`.`order_id` ASC",
            buildOrdersUsersSubsettingQuery(t, "100 > user_id", sqlmanager_shared.MysqlDriver),
        )
    })
}

Environment

  • MySQL driver
  • Go version: latest
  • OS: any

Additional context

  • Only affects MySQL queries
  • Only happens when column is on right side of comparison
  • PostgreSQL queries work correctly in all cases due to different AST traversal approach
@kmirzavaziri kmirzavaziri added the bug Something isn't working label Apr 18, 2025
@kmirzavaziri
Copy link
Author

kmirzavaziri commented Apr 18, 2025

I've submitted a PR to fix this issue: #3495

cc @nickzelei @evisdrenova

@evisdrenova
Copy link
Contributor

Thanks! Will review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants