orm:schema-tool:drop does not escape identifiers properly #5496

Open
opened 2026-01-22 15:09:10 +01:00 by admin · 2 comments
Owner

Originally created by @MatthiasKuehneEllerhold on GitHub (Apr 7, 2017).

Using the orm:schema-tool:drop CLI Command does not work properly because identifiers dont get escaped correctly.

Create test database:

CREATE TABLE test1 (id INT, PRIMARY KEY (id));
CREATE TABLE test2 (id INT, test1 INT, PRIMARY KEY (id));
ALTER TABLE test2 ADD CONSTRAINT `test2-test1` FOREIGN KEY(test1) REFERENCES test1 (id);

Execute the CLI Command in bash:

$ vendor/bin/doctrine-module orm:schema-tool:drop --dump-sql --full-database
ALTER TABLE test2 DROP FOREIGN KEY test2-test1;
DROP TABLE test1;
DROP TABLE test2;

$ vendor/bin/doctrine-module orm:schema-tool:drop --full-database --force
Dropping database schema...
  [Doctrine\DBAL\Exception\SyntaxErrorException]
  An exception occurred while executing 'ALTER TABLE test2 DROP FOREIGN KEY test2-test1': SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '-test1' at line 1

Properly escaping the foreign key would have worked:

ALTER TABLE test2 DROP FOREIGN KEY `test2-test1`;

This was used in a ZF3 Project with the doctrine-orm-module, but the drop command is in Doctrine\ORM here: https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/DropCommand.php

Versions:

doctrine/annotations                         v1.4.0
doctrine/cache                               v1.6.1
doctrine/collections                         v1.4.0
doctrine/common                              v2.7.2
doctrine/dbal                                v2.5.12
doctrine/doctrine-module                     1.2.0
doctrine/doctrine-orm-module                 1.1.1
doctrine/inflector                           v1.1.0
doctrine/instantiator                        1.0.5
doctrine/lexer                               v1.0.1
doctrine/migrations                          v1.5.0
doctrine/orm                                 v2.5.6
Originally created by @MatthiasKuehneEllerhold on GitHub (Apr 7, 2017). Using the `orm:schema-tool:drop` CLI Command does not work properly because identifiers dont get escaped correctly. Create test database: ``` CREATE TABLE test1 (id INT, PRIMARY KEY (id)); CREATE TABLE test2 (id INT, test1 INT, PRIMARY KEY (id)); ALTER TABLE test2 ADD CONSTRAINT `test2-test1` FOREIGN KEY(test1) REFERENCES test1 (id); ``` Execute the CLI Command in bash: ``` $ vendor/bin/doctrine-module orm:schema-tool:drop --dump-sql --full-database ALTER TABLE test2 DROP FOREIGN KEY test2-test1; DROP TABLE test1; DROP TABLE test2; $ vendor/bin/doctrine-module orm:schema-tool:drop --full-database --force Dropping database schema... [Doctrine\DBAL\Exception\SyntaxErrorException] An exception occurred while executing 'ALTER TABLE test2 DROP FOREIGN KEY test2-test1': SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '-test1' at line 1 ``` Properly escaping the foreign key would have worked: ``` ALTER TABLE test2 DROP FOREIGN KEY `test2-test1`; ``` This was used in a ZF3 Project with the doctrine-orm-module, but the drop command is in Doctrine\ORM here: https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/DropCommand.php Versions: ``` doctrine/annotations v1.4.0 doctrine/cache v1.6.1 doctrine/collections v1.4.0 doctrine/common v2.7.2 doctrine/dbal v2.5.12 doctrine/doctrine-module 1.2.0 doctrine/doctrine-orm-module 1.1.1 doctrine/inflector v1.1.0 doctrine/instantiator 1.0.5 doctrine/lexer v1.0.1 doctrine/migrations v1.5.0 doctrine/orm v2.5.6 ```
admin added the BugMissing Tests labels 2026-01-22 15:09:10 +01:00
Author
Owner

@theofidry commented on GitHub (Apr 7, 2017):

I think this is the same issue as https://github.com/doctrine/doctrine2/issues/5874 in which case this cannot be fixed automatically in 2.x.

@theofidry commented on GitHub (Apr 7, 2017): I think this is the same issue as https://github.com/doctrine/doctrine2/issues/5874 in which case this cannot be fixed automatically in 2.x.
Author
Owner

@Ocramius commented on GitHub (Apr 8, 2017):

@theofidry that applies to columns, not to constraint names. I think this is a DBAL issue to be reported/tested there.

@Ocramius commented on GitHub (Apr 8, 2017): @theofidry that applies to columns, not to constraint names. I think this is a DBAL issue to be reported/tested there.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5496