From 61afdd92ddb2d8a1e7d2240f8e0ded65f778e5f1 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 30 Sep 2018 20:15:01 +0800 Subject: [PATCH] Add foreign key constraint for creator_id This foreign key constrains will prevents creator record deletion from users table. --- src/stubs/database/migrations/migration-create.stub | 2 ++ tests/Generators/MigrationGeneratorTest.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/stubs/database/migrations/migration-create.stub b/src/stubs/database/migrations/migration-create.stub index ab637f0..e3988e0 100755 --- a/src/stubs/database/migrations/migration-create.stub +++ b/src/stubs/database/migrations/migration-create.stub @@ -19,6 +19,8 @@ class CreateMastersTable extends Migration $table->string('description')->nullable(); $table->unsignedInteger('creator_id'); $table->timestamps(); + + $table->foreign('creator_id')->references('id')->on('users')->onDelete('restrict'); }); } diff --git a/tests/Generators/MigrationGeneratorTest.php b/tests/Generators/MigrationGeneratorTest.php index 0fc2838..570a504 100644 --- a/tests/Generators/MigrationGeneratorTest.php +++ b/tests/Generators/MigrationGeneratorTest.php @@ -34,6 +34,8 @@ class Create{$this->plural_model_name}Table extends Migration \$table->string('description')->nullable(); \$table->unsignedInteger('creator_id'); \$table->timestamps(); + + \$table->foreign('creator_id')->references('id')->on('users')->onDelete('restrict'); }); }