diff --git a/src/Illuminate/Database/Schema/Grammars/Grammar.php b/src/Illuminate/Database/Schema/Grammars/Grammar.php index 2d2de55bea69..7fd6b4a2d9a2 100755 --- a/src/Illuminate/Database/Schema/Grammars/Grammar.php +++ b/src/Illuminate/Database/Schema/Grammars/Grammar.php @@ -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 primary key.'); + } + /** * Compile a fulltext index key command. * diff --git a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php index 2c2656eaaa9f..82a0091eeba6 100644 --- a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php @@ -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) ); } @@ -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.'); } /** diff --git a/tests/Database/DatabaseSQLiteSchemaGrammarTest.php b/tests/Database/DatabaseSQLiteSchemaGrammarTest.php index 7eee8a9d295b..ebbc0e316e38 100755 --- a/tests/Database/DatabaseSQLiteSchemaGrammarTest.php +++ b/tests/Database/DatabaseSQLiteSchemaGrammarTest.php @@ -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() @@ -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); }