laravel - 如何解决这个错误 SQLSTATE[HY000]: General error: 1005 Can't create table
问题描述
运行迁移时出现此错误
SQLSTATE[HY000]:一般错误:1005 无法创建表
tms-app
。#sql-1e64_2b
(errno: 150 "Foreign key constraint is wrongly forms") (SQL: alter tableprojects
add constraintprojects_cout_id_foreign
foreign key (cout_id
) referencescouts
(id
) on update cascade)
这是项目表:
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('libelle');
$table->string('libelle_long');
$table->string('direction');
$table->integer('cout_id')->unsigned();
$table->foreign('cout_id')
->references('id')->on('couts')
->onUpdate('cascade');
$table->foreign('status')
->referenecs('id')->on('statuses')
->onUpdate('cascade')
->onDelete('cascade');
$table->timestamps();
});
解决方案
有时会发生此错误,当我们定义外键并且您应该使用bigInteger('')
或使用unsignedBigInteger('')
.
使用下面的代码:
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('libelle');
$table->string('libelle_long');
$table->string('direction');
$table->bigInteger('cout_id')->unsigned();
$table->bigInteger('status')->unsigned();
$table->foreign('cout_id')->references('id')->on('couts')->onDelete('cascade');
$table->foreign('status')->references('id')->on('statuses')->onDelete('cascade');
$table->timestamps();
});
注意:在表格中couts
&statuses
将 id 字段更改$table->increments('id');
为$table->bigIncrements('id');
推荐阅读
- javascript - 我可以在 javascript 中获得对 toLowerCase 方法的引用,而无需在其上编写包装器吗?
- react-router - 如何使用反应路由器 6 在反应组件之外导航
- python-3.x - 格式化 ArgParse 帮助菜单
- command - 几次交流后,Raspberry Pi 3 上的 Google Assistant 停止响应
- arrays - Google 表格数组公式不适用于该列的其余部分
- javascript - 是否可以在运行时修改 ECMAScript 模块源代码(并更改其行为)?
- javascript - 使用 Preact 时媒体查询没有反映
- pygame - Python,在函数之间传递变量的正确方法
- asp.net - 如何正确使用 ASP.NET Core Web Application React 项目模板
- python - 当我使用 AND 运算符时,sqlite3 没有选择任何东西