Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixes a bug caused by unquoted reserved table name being referenced during schema creation #166

Closed
wants to merge 2 commits into from

7 participants

@klaussilveira

When creating a schema with tables with reserved names and relationships between them, the schema creation tool failed to write valid SQL in MySQL due to unquoted table names. For example:

CREATE TABLE `Group` (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL DEFAULT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE User (id INT AUTO_INCREMENT NOT NULL, username VARCHAR(255) NOT NULL PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE UserHasGroup (user_id INT NOT NULL, group_id INT NOT NULL, INDEX IDX_617A865CA76ED395 (user_id), INDEX IDX_617A865CFE54D947 (group_id), PRIMARY KEY(user_id, group_id)) ENGINE = InnoDB;
ALTER TABLE UserHasGroup ADD CONSTRAINT FK_617A865CA76ED395 FOREIGN KEY (user_id) REFERENCES User (id);
ALTER TABLE UserHasGroup ADD CONSTRAINT FK_617A865CFE54D947 FOREIGN KEY (group_id) REFERENCES Group (id);

This fix creates a small function for creating quoted foreign key table names.

@travisbot

This pull request fails (merged fe9d93d into ab57a01).

@asm89
Collaborator

Can you run the testsuite locally? Your PR breaks a lot.

@stof stof commented on the diff
lib/Doctrine/DBAL/Schema/ForeignKeyConstraint.php
@@ -110,6 +110,24 @@ public function getForeignTableName()
}
/**
+ * Get the quoted representation of this asset but only if it was defined with one. Otherwise
+ * return the plain unquoted value as inserted.
+ *
+ * @param AbstractPlatform $platform
+ * @return string
+ */
+ public function getQuotedForeignTableName(AbstractPlatform $platform)
@stof
stof added a note

the use statement is missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@klaussilveira

Fixed the missing use statement. Everything should work fine now.

@travisbot

This pull request fails (merged 82af3c0 into ab57a01).

@klaussilveira

Since this was marked as not a bug, closing the request.

@beberlei
Owner

The ticket was marked Awaiting Feedback, not closed. I applied this patch now, thank you very much!

@drgomesp

Any updates on this?

@deeky666
Collaborator

@drgomesp What do you mean by "updates"? The PR has already been merged a long time ago.

@stof

@drgomesp The patch was applied 1 year ago: 311bda5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 5, 2012
  1. @klaussilveira
Commits on Jul 10, 2012
  1. @klaussilveira
This page is out of date. Refresh to see the latest.
View
2  lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
@@ -2004,7 +2004,7 @@ public function getForeignKeyBaseDeclarationSQL(ForeignKeyConstraint $foreignKey
$sql .= implode(', ', $foreignKey->getLocalColumns())
. ') REFERENCES '
- . $foreignKey->getForeignTableName() . ' ('
+ . $foreignKey->getQuotedForeignTableName($this) . ' ('
. implode(', ', $foreignKey->getForeignColumns()) . ')';
return $sql;
View
19 lib/Doctrine/DBAL/Schema/ForeignKeyConstraint.php
@@ -22,6 +22,7 @@
namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Schema\Visitor\Visitor;
+use Doctrine\DBAL\Platforms\AbstractPlatform;
class ForeignKeyConstraint extends AbstractAsset implements Constraint
{
@@ -110,6 +111,24 @@ public function getForeignTableName()
}
/**
+ * Get the quoted representation of this asset but only if it was defined with one. Otherwise
+ * return the plain unquoted value as inserted.
+ *
+ * @param AbstractPlatform $platform
+ * @return string
+ */
+ public function getQuotedForeignTableName(AbstractPlatform $platform)
@stof
stof added a note

the use statement is missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ $keywords = $platform->getReservedKeywordsList();
+ $parts = explode(".", $this->getForeignTableName());
+ foreach ($parts AS $k => $v) {
+ $parts[$k] = ($this->_quoted || $keywords->isKeyword($v)) ? $platform->quoteIdentifier($v) : $v;
+ }
+
+ return implode(".", $parts);
+ }
+
+ /**
* @return array
*/
public function getForeignColumns()
Something went wrong with that request. Please try again.