php – laravel errno 150外键约束形成错误
发布时间:2020-12-14 19:38:11 所属栏目:大数据 来源:网络整理
导读:有人可以帮我解决这个问题吗? 有3个表有2个外键: Schema::create('users',function (Blueprint $table) { $table-increments('id'); $table-string('name'); $table-string('email')-unique(); $table-string('password'); $table-rememberToken(); $table
有人可以帮我解决这个问题吗?
有3个表有2个外键: Schema::create('users',function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); Schema::create('firms',function (Blueprint $table) { $table->increments('id'); $table->string('title')->nullable(); $table->integer('user_id')->unsigned()->nullable(); $table->foreign('user_id')->references('id')->on('users'); $table->timestamps(); }); Schema::create('jobs',function (Blueprint $table) { $table->increments('id'); $table->string('title')->nullable(); $table->integer('firm_id')->unsigned()->nullable(); $table->foreign('firm_id')->references('id')->on('firms'); $table->timestamps(); }); 运行迁移后出错: [IlluminateDatabaseQueryException] SQLSTATE[HY000]: General error: 1005 Can't create table `job`.`#sql-5fc_a1` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter ta ble `firms` add constraint `firms_user_id_foreign` foreign key (`user_id`) references `users` (`id`)) [PDOException] SQLSTATE[HY000]: General error: 1005 Can't create table `job`.`#sql-5fc_a1` (errno: 150 "Foreign key constraint is incorrectly formed")
对于外键,引用和引用字段必须具有完全相同的数据类型.
您可以在用户和公司中创建id字段作为有符号整数.但是,您将两个外键都创建为无符号整数,因此密钥的创建失败. 您需要将unsigned子句添加到id字段定义,或者从外键字段中删除unsigned子句. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |