Doctrine DBAL
  1. Doctrine DBAL
  2. DBAL-18

Doctrine\DBAL\Schema\SchemaDiff generates foreign keys statments for new tables even if the database platform doesn't support them

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.0.0-BETA2
    • Component/s: None
    • Labels:
      None
    • Environment:
      MacOS X 10.5, PHP 5.3.2

      Description

      When running the code snippets of <http://www.doctrine-project.org/projects/dbal/2.0/docs/reference/schema-representation/en>, I found that Doctrine\DBAL\Schema\SchemaDiff generates foreign keys statments for new tables even if the database plataform doesn't support them. My connection is a pdo_sqlite.

      <?php
      $schema = new \Doctrine\DBAL\Schema\Schema();
      
      // Doctrine\DBAL\Schema\Schema pode ser usado para criar representações do
      // schema, como a criação de tabelas
      $myTable = $schema->createTable('my_table');
      $myTable->addColumn('id', 'integer', array('unsigned' => true));
      $myTable->addColumn("username", "string", array("length" => 32));
      $myTable->setPrimaryKey(array("id"));
      $myTable->addUniqueIndex(array("username"));
      
      // SQLite does not supports sequences.
      //$schema->createSequence("my_table_seq");
      
      // Clone just to test Doctrine\DBAL\Schema\Comparator
      $fromSchema = clone $schema;
      
      $myForeign = $schema->createTable("my_foreign");
      $myForeign->addColumn("id", "integer");
      $myForeign->addColumn("user_id", "integer");
      $myForeign->addForeignKeyConstraint($myTable, array("user_id"), array("id"), array("onUpdate" => "CASCADE"));
      
      $queries = $schema->toSql($conn1->getDatabasePlatform()); // get queries to create this schema.
      $dropSchema = $schema->toDropSql($conn1->getDatabasePlatform()); // get queries to safely delete this schema.
      
      var_dump($queries);
      var_dump($dropSchema);
      
      
      $comparator = new \Doctrine\DBAL\Schema\Comparator();
      $schemaDiff = $comparator->compare($fromSchema, $schema);
      
      $queries     = $schemaDiff->toSql($conn1->getDatabasePlatform());     // queries to get from one to another schema.
      $saveQueries = $schemaDiff->toSaveSql($conn1->getDatabasePlatform());
      
      var_dump($queries);
      var_dump($saveQueries);
      ?>
      

      Both var_dump calls will output something like this:

      array(2) {
        [0]=>
        string(71) "CREATE TABLE my_foreign (user_id INTEGER NOT NULL, id INTEGER NOT NULL)"
        [1]=>
        string(105) "ALTER TABLE my_foreign ADD CONSTRAINT my_foreign_user_id_fk FOREIGN KEY (user_id) REFERENCES my_table(id)"
      }
      

        Activity

        Hide
        Eriksen Costa added a comment -

        I have this fixed in mine github fork, 'DBAL-18' topic branch: <http://github.com/eriksencosta/dbal/tree/DBAL-18>.

        I added an extra if statment to check if the platform supports foreign keys constraints and updated the test case also.

        In the test case, I didn't created an extra test method because I found it very related to the first if statment of the method Doctrine\DBAL\Schema\SchemaDiff::_toSql().

        Show
        Eriksen Costa added a comment - I have this fixed in mine github fork, ' DBAL-18 ' topic branch: < http://github.com/eriksencosta/dbal/tree/DBAL-18 >. I added an extra if statment to check if the platform supports foreign keys constraints and updated the test case also. In the test case, I didn't created an extra test method because I found it very related to the first if statment of the method Doctrine\DBAL\Schema\SchemaDiff::_toSql() .
        Hide
        Benjamin Eberlei added a comment -

        Merged into DBAL Master, will be released with BETA 2

        Show
        Benjamin Eberlei added a comment - Merged into DBAL Master, will be released with BETA 2

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Eriksen Costa
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: