Skip to content

Commit 6128f20

Browse files
feature/add-migration-docs (#17)
* Added database migrations section to readme * Added foreign key example
1 parent 70d13f1 commit 6128f20

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,52 @@ command (with exception of not being able to create a pivot model):
177177
php artisan uuid:make:model Models/Post --all
178178
```
179179

180+
### Database migrations
181+
182+
The default primary ID column used in migrations will not work with UUID primary
183+
keys, as the default column type is an unsigned integer. UUIDs are 36 character
184+
strings so we must specify this in our migrations:
185+
186+
```php
187+
<?php
188+
189+
use Illuminate\Database\Migrations\Migration;
190+
use Illuminate\Database\Schema\Blueprint;
191+
use Illuminate\Support\Facades\Schema;
192+
193+
class CreateUsersTable extends Migration
194+
{
195+
/**
196+
* Run the migrations.
197+
*/
198+
public function up(): void
199+
{
200+
Schema::create('users', function (Blueprint $table): void {
201+
// Primary key.
202+
$table->uuid('id')->primary();
203+
});
204+
}
205+
}
206+
207+
class CreatePostsTable extends Migration
208+
{
209+
/**
210+
* Run the migrations.
211+
*/
212+
public function up(): void
213+
{
214+
Schema::create('posts', function (Blueprint $table): void {
215+
// Primary key.
216+
$table->uuid('id')->primary();
217+
218+
// Foreign key.
219+
$table->uuid('user_id');
220+
$table->foreign('user_id')->references('id')->on('users');
221+
});
222+
}
223+
}
224+
```
225+
180226
## Running the tests
181227

182228
To run the test suite you can use the following commands:

0 commit comments

Comments
 (0)