迁移:无法在 Laravel 6 中添加外键约束
Migration: Cannot add foreign key constraint in Laravel 6
我正在尝试在 Laravel 中创建外键。但是,当我使用 Artisan 迁移 table 时,出现以下错误。
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error:
1215 Cannot add foreign key constraint (SQL: alter table posts
add
constraint posts_category_id_foreign
foreign key (category_id
)
references categories
(id
))
帖子迁移
Schema::create('posts', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('title');
$table->string('slug')->unique();
$table->longText('content');
$table->string('image')->nullable();
$table->uuid('author_id');
$table->uuid('category_id');
$table->timestamps();
$table->foreign('author_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('set null');
});
类别迁移
Schema::create('categories', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('slug')->unique();
$table->uuid('parent')->nullable();
});
将您的外键声明拆分为它们自己的架构方法...
我不明白问题的原因,但这样做对我来说一直有效。
Schema::create('posts', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('title');
$table->string('slug')->unique();
$table->longText('content');
$table->string('image')->nullable();
$table->uuid('author_id');
$table->uuid('category_id');
$table->timestamps();
});
Schema::table('posts', function(Blueprint $table) {
$table->foreign('author_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('set null');
});
在您的 posts
架构中,您在删除时将 author_id
和 category_id
设置为空,但您没有将这些字段设置为可为空。将定义更改为:
$table->uuid('author_id')->nullable();
$table->uuid('category_id')->nullable();
应该这样做。
我正在尝试在 Laravel 中创建外键。但是,当我使用 Artisan 迁移 table 时,出现以下错误。
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table
posts
add constraintposts_category_id_foreign
foreign key (category_id
) referencescategories
(id
))
帖子迁移
Schema::create('posts', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('title');
$table->string('slug')->unique();
$table->longText('content');
$table->string('image')->nullable();
$table->uuid('author_id');
$table->uuid('category_id');
$table->timestamps();
$table->foreign('author_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('set null');
});
类别迁移
Schema::create('categories', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('slug')->unique();
$table->uuid('parent')->nullable();
});
将您的外键声明拆分为它们自己的架构方法...
我不明白问题的原因,但这样做对我来说一直有效。
Schema::create('posts', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('title');
$table->string('slug')->unique();
$table->longText('content');
$table->string('image')->nullable();
$table->uuid('author_id');
$table->uuid('category_id');
$table->timestamps();
});
Schema::table('posts', function(Blueprint $table) {
$table->foreign('author_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('set null');
});
在您的 posts
架构中,您在删除时将 author_id
和 category_id
设置为空,但您没有将这些字段设置为可为空。将定义更改为:
$table->uuid('author_id')->nullable();
$table->uuid('category_id')->nullable();
应该这样做。