当我进行php artisan迁移时出现此错误。我的迁移文件有问题吗?
Illuminate \ Database \ QueryException:SQLSTATE [HY000]:常规错误: 1005无法创建表
test
。blog_posts
(错误号:150“外键 约束的格式不正确”)(SQL:alter tableblog_posts
add 约束blog_pos ts_user_id_foreign
外键(user_id
) 引用users
(id
))
博客文章
$table->increments('id');
$table->integer('cаtegory_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->string('slug')->unique();
$table->string('title');
$table->text('excerpt')->nullable(); դաշտ չի
$table->text('content_raw');
$table->text('content_html');
$table->boolean('is_published')->default(false);
$table->timestamp('published_at')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('cаtegory_id')->references('id')->on('blog_categories');
$table->index('is_published');
博客类别
$table->increments('id');
$table->integer('parent_id')->unsigned()->default(0);
$table->string('slug')->unique();
$table->string('title');
$table->text('description')->unllable();
$table->timestamps();
$table->softDeletes();
用户
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
答案 0 :(得分:1)
您需要将用户表中的列类型和格式匹配到FK中。但是,您首先需要在blog_posts
表上实际创建一列,从中绘制FK以使其完全匹配用户表上的内容
$table->foreign('user_id')->references('id')->on('users');
是FK指令,但是在此之前,您需要添加实际列以匹配users表。 Nullable是可选的,具体取决于您要如何进行:
$table->bigInteger('user_id')->unsigned()->nullable();