diff --git a/src/Resolvers/Resolver.php b/src/Resolvers/Resolver.php index 5d591e1..b4d9020 100644 --- a/src/Resolvers/Resolver.php +++ b/src/Resolvers/Resolver.php @@ -63,6 +63,10 @@ public function last(string $migration): string $migration .= '->default("'.$this->column->default.'")'; } + if ($this->column->primaryKey === true && $this->column->field !== 'id') { + $migration .= '->primary()'; + } + return $migration.';'; } diff --git a/tests/Unit/Generators/ModelGeneratorTest.php b/tests/Unit/Generators/ModelGeneratorTest.php index 79e3ca8..f2bc546 100644 --- a/tests/Unit/Generators/ModelGeneratorTest.php +++ b/tests/Unit/Generators/ModelGeneratorTest.php @@ -104,4 +104,20 @@ public function test_primary_key_is_empty_when_field_name_is_id(): void $this->assertStringNotContainsString('primaryKey', $file); } + + public function test_primary_key_is_empty_when_field_name_is_not_id(): void + { + File::system()->delete(Path::testgen().DIRECTORY_SEPARATOR.'Sample.php'); + + ModelGenerator::make( + new Table('samples', [ + Column::make('play_code', 'varchar', unsigned: false, autoIncrement: false, primaryKey: true), + ]), + destination: Path::testgen() + )->run(); + + $file = File::system()->read(Path::testgen().DIRECTORY_SEPARATOR.'Sample.php'); + + $this->assertStringContainsString('protected $primaryKey = \'play_code\';', $file); + } }