大佬教程收集整理的这篇文章主要介绍了SQLSTATE[HY000]:一般错误:1005 无法创建表`ic`.`livros`(errno: 150 "外键约束的格式不正确") id`))),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想从表“users”创建带有外键“users_ID”的表“livros”,但我无法迁移。
表格:
public function up()
{
scheR_445_11845@a::create('livros',function (Blueprint $tablE) {
$table->ID();
$table->Integer('users_ID');
$table->foreign('users_ID')->references('ID')->on('users');
/*Auth::user()->ID;*/
$table->text('namel');
$table->String('autor');
$table->String('editora');
$table->String('categoria');
$table->String('classificação');
$table->text('descricao');
$table->String('image')->nullable();
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
});
}
您的外键需要与您的父键类型相同,users
表 $table->id();
中的父键是一个无符号大整数,因此您的外键也要同类型,改:
$table->Integer('users_id');
$table->foreign('users_id')->references('id')->on('users');
到
$table->unsignedBigInteger('users_id');
$table->foreign('users_id')->references('id')->on('users');
或者,使用 foreignId
$table->foreignId('user_id')->consTrained();
以上是大佬教程为你收集整理的SQLSTATE[HY000]:一般错误:1005 无法创建表`ic`.`livros`(errno: 150 "外键约束的格式不正确") id`)))全部内容,希望文章能够帮你解决SQLSTATE[HY000]:一般错误:1005 无法创建表`ic`.`livros`(errno: 150 "外键约束的格式不正确") id`)))所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。