Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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
asm89 commented July 07, 2012

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

Christophe Coevoet stof commented on the diff July 07, 2012
lib/Doctrine/DBAL/Schema/ForeignKeyConstraint.php
@@ -110,6 +110,24 @@ public function getForeignTableName()
110 110
     }
111 111
 
112 112
     /**
  113
+     * Get the quoted representation of this asset but only if it was defined with one. Otherwise
  114
+     * return the plain unquoted value as inserted.
  115
+     *
  116
+     * @param AbstractPlatform $platform
  117
+     * @return string
  118
+     */
  119
+    public function getQuotedForeignTableName(AbstractPlatform $platform)
1
Christophe Coevoet
stof added a note July 07, 2012

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.

Klaus Silveira klaussilveira closed this July 19, 2012
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
This page is out of date. Refresh to see the latest.
2  lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
@@ -2004,7 +2004,7 @@ public function getForeignKeyBaseDeclarationSQL(ForeignKeyConstraint $foreignKey
2004 2004
 
2005 2005
         $sql .= implode(', ', $foreignKey->getLocalColumns())
2006 2006
               . ') REFERENCES '
2007  
-              . $foreignKey->getForeignTableName() . ' ('
  2007
+              . $foreignKey->getQuotedForeignTableName($this) . ' ('
2008 2008
               . implode(', ', $foreignKey->getForeignColumns()) . ')';
2009 2009
 
2010 2010
         return $sql;
19  lib/Doctrine/DBAL/Schema/ForeignKeyConstraint.php
@@ -22,6 +22,7 @@
22 22
 namespace Doctrine\DBAL\Schema;
23 23
 
24 24
 use Doctrine\DBAL\Schema\Visitor\Visitor;
  25
+use Doctrine\DBAL\Platforms\AbstractPlatform;
25 26
 
26 27
 class ForeignKeyConstraint extends AbstractAsset implements Constraint
27 28
 {
@@ -110,6 +111,24 @@ public function getForeignTableName()
110 111
     }
111 112
 
112 113
     /**
  114
+     * Get the quoted representation of this asset but only if it was defined with one. Otherwise
  115
+     * return the plain unquoted value as inserted.
  116
+     *
  117
+     * @param AbstractPlatform $platform
  118
+     * @return string
  119
+     */
  120
+    public function getQuotedForeignTableName(AbstractPlatform $platform)
  121
+    {
  122
+        $keywords = $platform->getReservedKeywordsList();
  123
+        $parts = explode(".", $this->getForeignTableName());
  124
+        foreach ($parts AS $k => $v) {
  125
+            $parts[$k] = ($this->_quoted || $keywords->isKeyword($v)) ? $platform->quoteIdentifier($v) : $v;
  126
+        }
  127
+
  128
+        return implode(".", $parts);
  129
+    }
  130
+
  131
+    /**
113 132
      * @return array
114 133
      */
115 134
     public function getForeignColumns()
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.