繁体   English   中英

MySQL 创建表:错误1005 errno:150“外键约束形成错误”

[英]MySQL create table: error 1005 errno: 150 “Foreign key constraint is incorrectly formed”

为什么 laravel 架构回复:

SQLSTATE[HY000]:一般错误:1005 无法创建表employee_management employees (errno: 150 "Foreign key constraint is wrongly forms") (SQL: alter table employees add constraint employees_city_id_foreign外键( city_id )引用city ( id ))

PDOException::("SQLSTATE[HY000]: 一般错误: 1005 无法创建表employee_management employees (errno: 150 "外键约束格式不正确")")

我的表:

  Schema::create('employees', function (Blueprint $table) {
            $table->increments('id', true);
            $table->string('lastname', 60);
            $table->string('firstname', 60);
            $table->string('middlename', 60);
            $table->string('address', 120);
            $table->integer('city_id')->unsigned();
            $table->integer('state_id')->unsigned();
            $table->integer('country_id')->unsigned();;
            $table->foreign('city_id')->references('id')->on('city');
            $table->foreign('state_id')->references('id')->on('state');
            $table->foreign('country_id')->references('id')->on('country');
            $table->char('zip', 10);
            $table->integer('age')->unsigned();
            $table->date('birthdate');
            $table->date('date_hired');
            $table->integer('department_id')->unsigned();
            $table->integer('division_id')->unsigned();
            // $table->integer('company_id')->unsigned();
            $table->foreign('department_id')->references('id')->on('department');
            $table->foreign('division_id')->references('id')->on('division');
            // $table->foreign('company_id')->references('id')->on('company');
            $table->string('picture', 60);
            $table->timestamps();
            $table->softDeletes();
        });

我的第二张城市表:

Schema::create('city', function (Blueprint $table) {
            $table->increments('id', true);
            $table->integer('state_id')->unsigned();
            $table->foreign('state_id')->references('id')->on('state');
            $table->string('name', 60);
            $table->timestamps();
            $table->softDeletes();
        });

PS我的Laravel版本是8.12

原因是您在创建city表之前尝试迁移employee表,这很重要,因为您的employee表通过外键依赖于city表。

要解决这个问题,首先尝试迁移城市表。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM