Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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

Klaus Silveira Don't Add Me To Your Organization a.k.a The Travis Bot Alexander Benjamin Eberlei Daniel Ribeiro Steve Müller Christophe Coevoet
Klaus Silveira

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.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request fails (merged fe9d93d into ab57a01).

Alexander
Collaborator

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

Christophe Coevoet 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)
Christophe Coevoet
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
Klaus Silveira

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

Don't Add Me To Your Organization a.k.a The Travis Bot

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

Klaus Silveira

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

Benjamin Eberlei
Owner

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

Daniel Ribeiro

Any updates on this?

Steve Müller
Collaborator

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

Christophe Coevoet

@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. Klaus Silveira
Commits on Jul 10, 2012
  1. Klaus Silveira
This page is out of date. Refresh to see the latest.
2  lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
View
@@ -2004,7 +2004,7 @@ public function getForeignKeyBaseDeclarationSQL(ForeignKeyConstraint $foreignKey
$sql .= implode(', ', $foreignKey->getLocalColumns())
. ') REFERENCES '
- . $foreignKey->getForeignTableName() . ' ('
+ . $foreignKey->getQuotedForeignTableName($this) . ' ('
. implode(', ', $foreignKey->getForeignColumns()) . ')';
return $sql;
19 lib/Doctrine/DBAL/Schema/ForeignKeyConstraint.php
View
@@ -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)
Christophe Coevoet
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.