Laravel General error: 1005 Can't create table
Laravel General error: 1005 Can't create table
我在执行 php artisan 迁移时收到此错误。我的迁移文件有问题吗?
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error:
1005 Can't create table test
.blog_posts
(errno: 150 "Foreign key
constraint is incorrectly formed") (SQL: alter table blog_posts
add
constraint blog_pos ts_user_id_foreign
foreign key (user_id
)
references users
(id
))
blog_posts
$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');
blog_categories
$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();
您需要将来自用户 table 的列类型和格式匹配到 FK 中。但是您首先需要实际创建 blog_posts
table 上的列,从中绘制 FK 以 与用户 table[=18= 完全匹配]:
$table->foreign('user_id')->references('id')->on('users');
是FK指令,但在此之前,需要添加实际的列来匹配用户table。 Nullable 是可选的,具体取决于您要如何进行:
$table->bigInteger('user_id')->unsigned()->nullable();
我在执行 php artisan 迁移时收到此错误。我的迁移文件有问题吗?
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table
test
.blog_posts
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tableblog_posts
add constraintblog_pos ts_user_id_foreign
foreign key (user_id
) referencesusers
(id
))
blog_posts
$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');
blog_categories
$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();
您需要将来自用户 table 的列类型和格式匹配到 FK 中。但是您首先需要实际创建 blog_posts
table 上的列,从中绘制 FK 以 与用户 table[=18= 完全匹配]:
$table->foreign('user_id')->references('id')->on('users');
是FK指令,但在此之前,需要添加实际的列来匹配用户table。 Nullable 是可选的,具体取决于您要如何进行:
$table->bigInteger('user_id')->unsigned()->nullable();