diff --git a/src/Phinx/Db/Adapter/SQLiteAdapter.php b/src/Phinx/Db/Adapter/SQLiteAdapter.php index 5c624fa33..665fd9bbe 100644 --- a/src/Phinx/Db/Adapter/SQLiteAdapter.php +++ b/src/Phinx/Db/Adapter/SQLiteAdapter.php @@ -1321,17 +1321,19 @@ protected function getAddForeignKeyInstructions(Table $table, ForeignKey $foreig $tmpTableName = $state['tmpTableName']; $indexes = $this->getIndexes($tableName); foreach (array_keys($indexes) as $indexName) { - $sql .= sprintf( - 'DROP INDEX %s%s; ', - $schema, - $this->quoteColumnName($indexName) - ); - $createIndexSQL = $this->getDeclaringIndexSQL($tableName, $indexName); - $sql .= preg_replace( - "/\b${tableName}\b/", - $tmpTableName, - $createIndexSQL - ); + if (strpos($indexName, 'sqlite_autoindex_') !== 0) { + $sql .= sprintf( + 'DROP INDEX %s%s; ', + $schema, + $this->quoteColumnName($indexName) + ); + $createIndexSQL = $this->getDeclaringIndexSQL($tableName, $indexName); + $sql .= preg_replace( + "/\b${tableName}\b/", + $tmpTableName, + $createIndexSQL + ); + } } $this->execute($sql); diff --git a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php index a1f584900..838025ac6 100644 --- a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php @@ -315,6 +315,33 @@ public function testCreateTableWithIndexesAndForeignKey() ); } + public function testCreateTableWithoutAutoIncrementingPrimaryKeyAndWithForeignKey() + { + $refTable = (new \Phinx\Db\Table('tbl_master', ['id' => false, 'primary_key' => 'id'], $this->adapter)) + ->addColumn('id', 'text'); + $refTable->create(); + + $table = (new \Phinx\Db\Table('tbl_child', ['id' => false, 'primary_key' => 'master_id'], $this->adapter)) + ->addColumn('master_id', 'text') + ->addForeignKey( + 'master_id', + 'tbl_master', + 'id', + ['delete' => 'NO_ACTION', 'update' => 'NO_ACTION', 'constraint' => 'fk_master_id'] + ); + $table->create(); + + $this->assertTrue($this->adapter->hasForeignKey('tbl_child', ['master_id'])); + + $row = $this->adapter->fetchRow( + "SELECT * FROM sqlite_master WHERE `type` = 'table' AND `tbl_name` = 'tbl_child'" + ); + $this->assertStringContainsString( + 'CONSTRAINT `fk_master_id` FOREIGN KEY (`master_id`) REFERENCES `tbl_master` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION', + $row['sql'] + ); + } + public function testAddPrimaryKey() { $table = new \Phinx\Db\Table('table1', ['id' => false], $this->adapter);