简体   繁体   English

SQLSTATE [HY000]:错误:150“外键约束形成错误

[英]SQLSTATE[HY000]: errno: 150 "Foreign key constraint is incorrectly formed

I try to make migration and my error is "errno: 150 "Foreign key constraint is incorrectly formed" I cant explain more than this but I should write smt for stack validate length 我尝试进行迁移,我的错误是“错误:150”外键约束形成错误“我无法解释更多但我应该写smt for stack validate length

and its my code: 和我的代码:

public function up()
{
    Schema::create('bus_lines', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('title',20)->collation('utf8_persian_ci');
        $table->unsignedInteger('code');
        $table->integer('start_station');
        $table->integer('end_station');
        $table->smallInteger('pub');
        $table->smallInteger('rmv');
        $table->timestamps();

        });
}


public function up()
{
    Schema::create('station_buses', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('title',30);
        $table->unsignedInteger('code');
        $table->timestamps();

    });
}

public function up()
{
    Schema::create('busline_stationbus', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedInteger('line_code');
        $table->unsignedInteger('station_code');
        $table->timestamps();


        $table->foreign('line_code')->references('code')->on('bus_lines')->onDelete('cascade');
        $table->foreign('station_code')->references('code')->on('station_buses')->onDelete('cascade');
    });
}

So if the foreign key is applied to a non primary key it has to be applied to a unique column: 因此,如果外键应用于非主键,则必须将其应用于唯一列:

A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; FOREIGN KEY约束不必仅链接到另一个表中的PRIMARY KEY约束; it can also be defined to reference the columns of a UNIQUE constraint in another table. 它也可以定义为引用另一个表中UNIQUE约束的列。

So your code in both tables should be this: 所以两个表中的code应该是这样的:

$table->unsignedInteger('code')->unique();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE[HY000]: 一般错误: 1005 Can't create table `test`.`members` (errno: 150 “Foreign key constraint is misformed”) - SQLSTATE[HY000]: General error: 1005 Can't create table `test`.`members` (errno: 150 “Foreign key constraint is incorrectly formed”) SQLSTATE[HY000]: 一般错误: 1005 Can't create table `school`.`posts` (errno: 150 "Foreign key constraint is wrongly forms") - SQLSTATE[HY000]: General error: 1005 Can't create table `school`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed") SQLSTATE[HY000]: General error: 1005 Can't create table `business`.`users` (errno: 150 "Foreign key constraint is incorrectly formed") Laravel 7 - SQLSTATE[HY000]: General error: 1005 Can't create table `business`.`users` (errno: 150 "Foreign key constraint is incorrectly formed") Laravel 7 错误1005(HY000):无法创建表carparkful。#sql-3b64_3(错误号:150“外键约束格式不正确”) - ERROR 1005 (HY000): Can't create table `carparkful`.`#sql-3b64_3` (errno: 150 “Foreign key constraint is incorrectly formed”) Laravel 5.4:SQLSTATE[HY000]:一般错误:1005 无法创建表“外键约束格式不正确” - Laravel 5.4: SQLSTATE[HY000]: General error: 1005 Can't create table "Foreign key constraint is incorrectly formed" (错误号:150“外键约束格式不正确”) - (errno: 150 “Foreign key constraint is incorrectly formed”) errno:150外键约束格式错误 - errno: 150 Foreign key constraint is incorrectly formed errno: 150 "外键约束的格式不正确") - errno: 150 "Foreign key constraint is incorrectly formed") errno:150“外键约束格式不正确” - errno: 150 “Foreign key constraint is incorrectly formed” (错误号:150“外键约束格式不正确”) - (errno: 150 “Foreign key constraint is incorrectly formed”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM