diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index 9ff160ee21f..442a1ea7050 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -709,6 +709,10 @@ private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $inde // Dropping primary keys requires to unset autoincrement attribute on the particular column first. foreach ($index->getColumns() as $columnName) { + if (! $diff->fromTable->hasColumn($columnName)) { + continue; + } + $column = $diff->fromTable->getColumn($columnName); if ($column->getAutoincrement() === true) { diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php index 4f326a8f125..e6b77999efc 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php @@ -473,6 +473,30 @@ public function testNamedPrimaryKey() "ALTER TABLE mytable ADD PRIMARY KEY (foo)", ), $sql); } + + public function testAlterPrimaryKeyWithNewColumn() + { + $table = new Table("yolo"); + $table->addColumn('pkc1', 'integer'); + $table->addColumn('col_a', 'integer'); + $table->setPrimaryKey(array('pkc1')); + + $comparator = new Comparator(); + $diffTable = clone $table; + + $diffTable->addColumn('pkc2', 'integer'); + $diffTable->dropPrimaryKey(); + $diffTable->setPrimaryKey(array('pkc1', 'pkc2')); + + $this->assertSame( + array( + 'ALTER TABLE yolo DROP PRIMARY KEY', + 'ALTER TABLE yolo ADD pkc2 INT NOT NULL', + 'ALTER TABLE yolo ADD PRIMARY KEY (pkc1, pkc2)', + ), + $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable)) + ); + } public function testInitializesDoctrineTypeMappings() {