Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Drop the primary key if it exists when adding a new primary key #49392

Merged
merged 2 commits into from Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Expand Up @@ -406,7 +406,7 @@ public function compileChange(Blueprint $blueprint, Fluent $command, Connection
*/
public function compilePrimary(Blueprint $blueprint, Fluent $command)
{
return sprintf('alter table %s add primary key %s(%s)',
return sprintf('alter table %s drop index if exists `PRIMARY`, add primary key %s(%s)',
$this->wrapTable($blueprint),
$command->algorithm ? 'using '.$command->algorithm : '',
$this->columnize($command->columns)
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Expand Up @@ -345,7 +345,7 @@ public function testAddingPrimaryKey()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add primary key (`foo`)', $statements[0]);
$this->assertSame('alter table `users` drop index if exists `PRIMARY`, add primary key (`foo`)', $statements[0]);
}

public function testAddingPrimaryKeyWithAlgorithm()
Expand All @@ -355,7 +355,7 @@ public function testAddingPrimaryKeyWithAlgorithm()
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add primary key using hash(`foo`)', $statements[0]);
$this->assertSame('alter table `users` drop index if exists `PRIMARY`, add primary key using hash(`foo`)', $statements[0]);
}

public function testAddingUniqueKey()
Expand Down