Doctrine 1
  1. Doctrine 1
  2. DC-158

Postgresql Import does not recognize all parts of a composite primary key as primary keys

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.0.13
    • Fix Version/s: 1.2.0-BETA1
    • Component/s: Import/Export
    • Labels:
      None
    • Environment:
      Doctrine 1.1 from current SVN with PostgreSQL 8.3.8

      Description

      If a PostgreSQL table has a composite primary key, Doctrine import ("generate-models-db") will only mark the first column of the composite key as primary. This is due to a bug in Doctrine_Import_Pgsql.

      I have attached a short patch against the current 1.1 branch (revision 6580). The version 1.2 seems to have the same problem.
      I have tested the patch with PostgreSQL 8.3.8.

        Activity

        Hide
        Moritz Breit added a comment -

        Small test case for this bug:

        Database schema:

        schema.sql
        CREATE TABLE testa (
          id bigint NOT NULL,
          CONSTRAINT testa_pkey PRIMARY KEY (id)
        );
        
        CREATE TABLE testb (
          id bigint NOT NULL,
          CONSTRAINT testb_pkey PRIMARY KEY (id)
        );
        
        CREATE TABLE testa_testb (
          testa bigint NOT NULL,
          testb bigint NOT NULL,
          CONSTRAINT testa_testb_pkey PRIMARY KEY (testa, testb),
          CONSTRAINT fktesta FOREIGN KEY (testa) REFERENCES testa (id),
          CONSTRAINT fktestb FOREIGN KEY (testb) REFERENCES testb (id)
        );
        

        This is a typical many-to-many relationship with a mapping table.
        Note that testa_testb has a composite primary key with two columns, testa and testb.

        Generated code before applying the patch (generateModelsFromDb):

        BaseTestaTestb.php
        abstract class BaseTestaTestb extends Doctrine_Record
        {
            public function setTableDefinition()
            {
                $this->setTableName('testa_testb');
                $this->hasColumn('testa', 'integer', 8, array(
                     'type' => 'integer',
                     'length' => 8,
                     'unsigned' => false,
                     'primary' => true,
                     ));
                $this->hasColumn('testb', 'integer', 8, array(
                     'type' => 'integer',
                     'length' => 8,
                     'unsigned' => false,
                     'notnull' => true,
                     'primary' => false, // This should be true
                     ));
            }
        ...
        

        Generated code after applying the patch:

        BaseTestaTestb.php
        abstract class BaseTestaTestb extends Doctrine_Record
        {
            public function setTableDefinition()
            {
                $this->setTableName('testa_testb');
                $this->hasColumn('testa', 'integer', 8, array(
                     'type' => 'integer',
                     'length' => 8,
                     'unsigned' => false,
                     'primary' => true,
                     ));
                $this->hasColumn('testb', 'integer', 8, array(
                     'type' => 'integer',
                     'length' => 8,
                     'unsigned' => false,
                     'primary' => true,
                     ));
            }
        ...
        
        Show
        Moritz Breit added a comment - Small test case for this bug: Database schema: schema.sql CREATE TABLE testa ( id bigint NOT NULL, CONSTRAINT testa_pkey PRIMARY KEY (id) ); CREATE TABLE testb ( id bigint NOT NULL, CONSTRAINT testb_pkey PRIMARY KEY (id) ); CREATE TABLE testa_testb ( testa bigint NOT NULL, testb bigint NOT NULL, CONSTRAINT testa_testb_pkey PRIMARY KEY (testa, testb), CONSTRAINT fktesta FOREIGN KEY (testa) REFERENCES testa (id), CONSTRAINT fktestb FOREIGN KEY (testb) REFERENCES testb (id) ); This is a typical many-to-many relationship with a mapping table. Note that testa_testb has a composite primary key with two columns, testa and testb. Generated code before applying the patch (generateModelsFromDb): BaseTestaTestb.php abstract class BaseTestaTestb extends Doctrine_Record { public function setTableDefinition() { $ this ->setTableName('testa_testb'); $ this ->hasColumn('testa', 'integer', 8, array( 'type' => 'integer', 'length' => 8, 'unsigned' => false , 'primary' => true , )); $ this ->hasColumn('testb', 'integer', 8, array( 'type' => 'integer', 'length' => 8, 'unsigned' => false , 'notnull' => true , 'primary' => false , // This should be true )); } ... Generated code after applying the patch: BaseTestaTestb.php abstract class BaseTestaTestb extends Doctrine_Record { public function setTableDefinition() { $ this ->setTableName('testa_testb'); $ this ->hasColumn('testa', 'integer', 8, array( 'type' => 'integer', 'length' => 8, 'unsigned' => false , 'primary' => true , )); $ this ->hasColumn('testb', 'integer', 8, array( 'type' => 'integer', 'length' => 8, 'unsigned' => false , 'primary' => true , )); } ...

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Moritz Breit
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: