From b5dab9908aa4edfe866b3f564abcba53d14c8989 Mon Sep 17 00:00:00 2001 From: MichaelDesignamite <116086171+MichaelDesignamite@users.noreply.github.com> Date: Sat, 7 Sep 2024 08:48:48 +0100 Subject: [PATCH] Fix setting sqlsrv PK name with qualified table name (#2306) Co-authored-by: Matthew Peveler --- src/Phinx/Db/Adapter/SqlServerAdapter.php | 2 +- tests/Phinx/Db/Adapter/SqlServerAdapterTest.php | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Phinx/Db/Adapter/SqlServerAdapter.php b/src/Phinx/Db/Adapter/SqlServerAdapter.php index cd9e104ee..8ac894398 100644 --- a/src/Phinx/Db/Adapter/SqlServerAdapter.php +++ b/src/Phinx/Db/Adapter/SqlServerAdapter.php @@ -270,7 +270,7 @@ public function createTable(Table $table, array $columns = [], array $indexes = // set the primary key(s) if (isset($options['primary_key'])) { - $pkSql = sprintf('CONSTRAINT PK_%s PRIMARY KEY (', $table->getName()); + $pkSql = sprintf('CONSTRAINT PK_%s PRIMARY KEY (', str_replace('.', '_', $table->getName())); if (is_string($options['primary_key'])) { // handle primary_key => 'id' $pkSql .= $this->quoteColumnName($options['primary_key']); } elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id') diff --git a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php index 20c88c0c3..cdc0e3088 100644 --- a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php @@ -199,6 +199,13 @@ public function testCreateTableWithNoPrimaryKey() $this->assertFalse($this->adapter->hasColumn('atable', 'id')); } + public function testCreateFullyQualifiedTable() + { + (new Table('dbo.qualified_table', [], $this->adapter))->create(); + $this->assertTrue($this->adapter->hasTable('dbo.qualified_table')); + $this->assertTrue($this->adapter->hasPrimaryKey('qualified_table', 'id')); + } + public function testCreateTableWithConflictingPrimaryKeys() { $options = [