Skip to content
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

Delete SQL statement is wrong when using alias MySQL #4448

Closed
ustmaestro opened this issue Nov 24, 2020 · 3 comments
Closed

Delete SQL statement is wrong when using alias MySQL #4448

ustmaestro opened this issue Nov 24, 2020 · 3 comments

Comments

@ustmaestro
Copy link

Bug Report

Invalid forming delete query when I use alias for table. Query produces a syntax error.

My MySQL version is 5.7.25.

Q A
BC Break no
Version 2.12.x

Summary

I'm using the delete like described in example https://github.com/doctrine/dbal/blob/79cba0bc0e653005fc9ab5a23624828a803f41fb/lib/Doctrine/DBAL/Query/QueryBuilder.php#L545

$qb = $conn->createQueryBuilder()
    ->delete('users_groups', 'ug')
    ->where('ug.groupId = :groupId')
    ->setParameter(':groupId', 1);

This creates a query like this: DELETE FROM users_groups ug WHERE ug.groupId = 1 that produces an MySQL error

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ug WHERE ug.groupId = 1' at line 1

I do not know if this is a specific MySQL issue or for some MySQL versions, my current MySQL version is 5.7.25.

I've searched MySQL error related answers and i found this https://stackoverflow.com/a/26935749/2624626

delete <alias> from <table> <alias> where <alias>.<field>...

For me DELETE ug FROM users_groups ug WHERE ug.groupId = 1 worked as expected and without errors.

Current behaviour

DELETE FROM <table_name> <alias> WHERE <alias>.<field> ...

How to reproduce

CREATE TABLE test_delete (userId int, groupId int);

INSERT INTO test_delete (userId, groupId) VALUES (1,1), (1,2), (1,3), (2,1), (2,3);

DELETE FROM test_delete td WHERE td.groupId = 1;

https://www.db-fiddle.com/f/uPFmoWKGyQngD97AVMBV8Y/1

DELETE td FROM test_delete td WHERE td.groupId = 1;

$qb = $conn->createQueryBuilder()
    ->delete('test_delete', 'td')
    ->where('td.groupId = :groupId')
    ->setParameter(':groupId', 1);

$qb->execute();

Expected behaviour

DELETE <alias> FROM <table_name> <alias> WHERE <alias>.<field> ...
@ustmaestro
Copy link
Author

ustmaestro commented Nov 24, 2020

A test for DELETE <alias> FROM <table_name> <alias> WHERE <alias>.<field> ...

https://www.db-fiddle.com/f/uPFmoWKGyQngD97AVMBV8Y/2

@simonberger
Copy link
Contributor

simonberger commented Dec 18, 2020

It looks like the platforms have the following syntax:

Additional alias In front of the FROM statement:

  • mysql
  • mariadb
  • mssql

Behind the table name:

  • PostgreSql

No alias / found no info:

  • others

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants