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

[11.x] Fix adding constraints on sqlite #49728

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ public function compileChange(Blueprint $blueprint, Fluent $command, Connection
throw new LogicException('This database driver does not support modifying columns.');
}

/**
* Compile a primary key command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compilePrimary(Blueprint $blueprint, Fluent $command)
{
throw new RuntimeException('This database driver does not support adding a primary key.');
}

/**
* Compile a fulltext index key command.
*
Expand Down
12 changes: 9 additions & 3 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,18 @@ public function compileForeignKeys($table)
*/
public function compileCreate(Blueprint $blueprint, Fluent $command)
{
$primaryKey = tap($this->getCommandByName($blueprint, 'primary'), fn ($pk) => $pk?->shouldBeSkipped());

$foreignKeys = tap($this->getCommandsByName($blueprint, 'foreign'),
fn ($commands) => collect($commands)->each->shouldBeSkipped()
);

return sprintf('%s table %s (%s%s%s)',
$blueprint->temporary ? 'create temporary' : 'create',
$this->wrapTable($blueprint),
implode(', ', $this->getColumns($blueprint)),
$this->addForeignKeys($this->getCommandsByName($blueprint, 'foreign')),
$this->addPrimaryKeys($this->getCommandByName($blueprint, 'primary'))
$this->addForeignKeys($foreignKeys),
$this->addPrimaryKeys($primaryKey)
);
}

Expand Down Expand Up @@ -419,7 +425,7 @@ public function compileSpatialIndex(Blueprint $blueprint, Fluent $command)
*/
public function compileForeign(Blueprint $blueprint, Fluent $command)
{
// Handled on table creation...
throw new RuntimeException('This database driver does not support adding foreign key constraints.');
}

/**
Expand Down
37 changes: 14 additions & 23 deletions tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,31 +306,31 @@ public function testAddingForeignID()
{
$blueprint = new Blueprint('users');
$foreignId = $blueprint->foreignId('foo');
$blueprint->foreignId('company_id')->constrained();
$blueprint->foreignId('laravel_idea_id')->constrained();
$blueprint->foreignId('team_id')->references('id')->on('teams');
$blueprint->foreignId('team_column_id')->constrained('teams');

$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignId);
$this->assertSame([
'alter table "users" add column "foo" integer not null',
'alter table "users" add column "company_id" integer not null',
'alter table "users" add column "laravel_idea_id" integer not null',
'alter table "users" add column "team_id" integer not null',
'alter table "users" add column "team_column_id" integer not null',
], $statements);
}

public function testAddingForeignIdSpecifyingIndexNameInConstraint()
public function testAddingForeignKeyConstraint()
{
$this->expectException(\RuntimeException::class);

$blueprint = new Blueprint('users');
$blueprint->foreignId('company_id')->constrained(indexName: 'my_index');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertSame([
'alter table "users" add column "company_id" integer not null',
], $statements);
$blueprint->foreignId('company_id')->constrained();
$blueprint->toSql($this->getConnection(), $this->getGrammar());
}

public function testAlterTableAddingPrimaryKey()
{
$this->expectException(\RuntimeException::class);

$blueprint = new Blueprint('users');
$blueprint->primary('id');
$blueprint->toSql($this->getConnection(), $this->getGrammar());
}

public function testAddingBigIncrementingID()
Expand Down Expand Up @@ -727,20 +727,11 @@ public function testAddingForeignUuid()
{
$blueprint = new Blueprint('users');
$foreignUuid = $blueprint->foreignUuid('foo');
$blueprint->foreignUuid('company_id')->constrained();
$blueprint->foreignUuid('laravel_idea_id')->constrained();
$blueprint->foreignUuid('team_id')->references('id')->on('teams');
$blueprint->foreignUuid('team_column_id')->constrained('teams');

$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertInstanceOf(ForeignIdColumnDefinition::class, $foreignUuid);
$this->assertSame([
'alter table "users" add column "foo" varchar not null',
'alter table "users" add column "company_id" varchar not null',
'alter table "users" add column "laravel_idea_id" varchar not null',
'alter table "users" add column "team_id" varchar not null',
'alter table "users" add column "team_column_id" varchar not null',
], $statements);
}

Expand Down