forked from summerblue/larabbs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b70dc5b
commit 1b4fdd8
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class AddReferences extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::table('topics', function (Blueprint $table) { | ||
|
||
// 当 user_id 对应的 users 表数据被删除时,删除词条 | ||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); | ||
}); | ||
|
||
Schema::table('replies', function (Blueprint $table) { | ||
|
||
// 当 user_id 对应的 users 表数据被删除时,删除此条数据 | ||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); | ||
|
||
// 当 topic_id 对应的 topics 表数据被删除时,删除此条数据 | ||
$table->foreign('topic_id')->references('id')->on('topics')->onDelete('cascade'); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::table('topics', function (Blueprint $table) { | ||
// 移除外键约束 | ||
$table->dropForeign(['user_id']); | ||
}); | ||
|
||
Schema::table('replies', function (Blueprint $table) { | ||
$table->dropForeign(['user_id']); | ||
$table->dropForeign(['topic_id']); | ||
}); | ||
|
||
} | ||
} |