Laravel 如何解决此erorr SQLSTATE[HY000]:一般错误:1005 Can';不创建表
运行迁移时出现此错误 SQLSTATE[HY000]:一般错误:1005无法创建表Laravel 如何解决此erorr SQLSTATE[HY000]:一般错误:1005 Can';不创建表,laravel,Laravel,运行迁移时出现此错误 SQLSTATE[HY000]:一般错误:1005无法创建表tms-app\sql-1e64\u 2b(errno:150“外键约束格式不正确”)(sql:alter-tableprojects添加约束projects\u-cout-id\u-Foreign外键(cout-id)引用couts(id)更新时(级联) 这是项目表: Schema::create('projects', function (Blueprint $table) { $t
tms-app
\sql-1e64\u 2b
(errno:150“外键约束格式不正确”)(sql:alter-tableprojects
添加约束projects\u-cout-id\u-Foreign
外键(cout-id
)引用couts
(id
)更新时(级联)
这是项目表:
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();
});
有时,当我们定义外键时,您应该使用biginger(“”)或使用unsignedbiginger(“”) 您可以使用下面给出的示例中的代码:
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();
});
在您的表中,更改您的$table->increments('id')到$table->大增量('id')此错误应该是由我提到的以下未完成配置引起的,请确保所有配置都正确 1:Laravel迁移外键应设为bigInteger或unsignedBigInteger 例子: $table->unsignedbiginger('user_id'); 或 $table->biginger('user_id')->unsigned() 对于分配外键属性,您应该这样做 $table->foreign('user_id')->reference('id')->on('users');//如果您需要进一步的属性,您可以链接到此行
2:确保迁移文件的顺序正确,因为Laravel将基于时间戳迁移表。例如,如果需要来自用户表的外键,则首先需要创建用户表,然后子表用户表的时间戳应比子表早(甚至1秒)欢迎使用。请添加
couts
table的迁移您需要在迁移projects
table之前迁移couts
table有时不需要,这是针对5.8及以上版本的,您还需要将增量更改为bigIncrements确保用户的表迁移发生在q&a表之前,因为q&a迁移文件引用用户表,所以它必须存在。通常,LaaFLE根据所创建的日期执行迁移文件,以便更好地考虑首先执行用户表,然后考虑Q&A。(SQL:ALTER表<代码>项目添加约束<代码>项目> StasuSUB/<代码>外键(<代码>状态< /代码>)引用<代码>状态>代码> >(在更新级联上删除级联)`我已经编辑了答案,你现在可以看到了。