Skip to content

Commit 4afaa50

Browse files
author
Carl Sverre
authored
Merge pull request #26 from srdante/without-primary-key
Add withoutPrimaryKey method for increment columns
2 parents 6f0e84c + 2681027 commit 4afaa50

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,20 @@ Schema::create('test', function (Blueprint $table) {
294294
});
295295
```
296296

297+
### Increment Columns without Primary Key
298+
299+
Sometimes you may want to set a custom primary key. However if your table has an int `increment` column,
300+
Laravel, by default, always sets this column as the primary key. Even if you manually set another one. This behavior can be disabled using the `withoutPrimaryKey` method.
301+
302+
```php
303+
Schema::create('test', function (Blueprint $table) {
304+
$table->id()->withoutPrimaryKey();
305+
$table->uuid('uuid');
306+
307+
$table->primaryKey(['id', 'uuid']);
308+
});
309+
```
310+
297311
## Testing
298312

299313
Execute the tests using PHPUnit

src/Schema/Grammar.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
class Grammar extends MySqlGrammar
1919
{
20-
use CompilesKeys, ModifiesColumns;
20+
use CompilesKeys;
21+
use ModifiesColumns;
2122

2223
public function __construct()
2324
{
@@ -64,7 +65,8 @@ protected function compileCreateTable($blueprint, $command, $connection)
6465
// We want to do as little as possible ourselves, so we rely on the parent
6566
// to compile everything and then potentially sneak some modifiers in.
6667
return $this->insertCreateTableModifiers(
67-
$blueprint, parent::compileCreateTable($blueprint, $command, $connection)
68+
$blueprint,
69+
parent::compileCreateTable($blueprint, $command, $connection)
6870
);
6971
}
7072

@@ -174,4 +176,20 @@ protected function compileKey(Blueprint $blueprint, Fluent $command, $type)
174176
// creating the indexes as a part of the create statement.
175177
return str_replace(sprintf('alter table %s add ', $this->wrapTable($blueprint)), '', $compiled);
176178
}
179+
180+
/**
181+
* Get the SQL for an auto-increment column modifier.
182+
*
183+
* @param \Illuminate\Database\Schema\Blueprint $blueprint
184+
* @param \Illuminate\Support\Fluent $column
185+
* @return string|null
186+
*/
187+
protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
188+
{
189+
if (in_array($column->type, $this->serials) && $column->autoIncrement) {
190+
return ($column->withoutPrimaryKey === true)
191+
? ' auto_increment'
192+
: ' auto_increment primary key';
193+
}
194+
}
177195
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* @author https://github.com/srdante
4+
*/
5+
6+
namespace SingleStore\Laravel\Tests\Hybrid\CreateTable;
7+
8+
use SingleStore\Laravel\Schema\Blueprint;
9+
use SingleStore\Laravel\Tests\BaseTest;
10+
use SingleStore\Laravel\Tests\Hybrid\HybridTestHelpers;
11+
12+
class IncrementWithoutPrimaryKeyTest extends BaseTest
13+
{
14+
use HybridTestHelpers;
15+
16+
/** @test */
17+
public function it_adds_a_big_increments_without_primary_key()
18+
{
19+
$blueprint = $this->createTable(function (Blueprint $table) {
20+
$table->bigIncrements('id')->withoutPrimaryKey();
21+
$table->uuid('uuid');
22+
23+
$table->primary(['id', 'uuid']);
24+
});
25+
26+
$this->assertCreateStatement(
27+
$blueprint,
28+
'create table `test` (`id` bigint unsigned not null auto_increment, `uuid` char(36) not null, primary key `test_id_uuid_primary`(`id`, `uuid`))'
29+
);
30+
}
31+
32+
/** @test */
33+
public function it_adds_an_id_without_primary_key()
34+
{
35+
$blueprint = $this->createTable(function (Blueprint $table) {
36+
$table->id()->withoutPrimaryKey();
37+
$table->uuid('uuid');
38+
39+
$table->primary(['id', 'uuid']);
40+
});
41+
42+
$this->assertCreateStatement(
43+
$blueprint,
44+
'create table `test` (`id` bigint unsigned not null auto_increment, `uuid` char(36) not null, primary key `test_id_uuid_primary`(`id`, `uuid`))'
45+
);
46+
}
47+
}

0 commit comments

Comments
 (0)