简体   繁体   English

常规错误:1005无法创建表errno:150“外键约束格式错误”)

[英]General error: 1005 Can't create table errno: 150 “Foreign key constraint is incorrectly formed”)

Blockquote SQLSTATE[HY000]: General error: 1005 Can't create table gps . Blockquote SQLSTATE [HY000]:常规错误:1005无法创建表gps #sql-9e4_161 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table receivers add constraint receivers_hospital_id_foreign foreign key ( hospital_id ) references hospitals ( id )) #sql-9e4_161 (错误:150 “被错误地形成的外键约束”)(SQL:ALTER TABLE receivers添加约束receivers_hospital_id_foreign外键( hospital_id )引用hospitalsid ))

{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamp('dob');
        $table->string('profile_image');
        $table->string('profession')->nullable();
        $table->string('email')->unique();
        $table->string('weight');
        $table->string('message');
        $table->rememberToken();
        $table->timestamps();
        $table->softDeletes();

    });


{
    Schema::create('receivers', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('member_id')->unsigned()->index();
        $table->integer('hospital_id')->unsigned()->index();
        $table->timestamps();
        $table->softDeletes();
        $table->foreign('member_id')->references('id')->on('members');
        $table->foreign('hospital_id')->references('id')->on('hospitals');
    });


{
    Schema::create('hospitals', function (Blueprint $table) {
        $table->increments('id');
        $table->string('h_name');
        $table->integer('country_id')->unsigned()->index();
        $table->integer('donate_id')->unsigned()->index();
        $table->softDeletes();
        $table->timestamps();
        $table->foreign('country_id')->references('id')->on('countries');
        $table->foreign('donate_id')->references('id')->on('donates');


    });


{
    Schema::create('donates', function (Blueprint $table) {
        $table->increments('id');
        $table->date('date');

        $table->date('last_bleed');
        $table->string('quantity');
        $table->string('comments');
        $table->integer('blood_group_id')->unsigned()->index();
        $table->integer('member_id')->unsigned()->index();

        $table->timestamps();
        $table->softDeletes();
        $table->foreign('blood_group_id')->references('id')-
                             >on('blood_groups');
        $table->foreign('member_id')->references('id')->on('members');
        $table->timestamps();
    });

}

I don't see member , blood_groups table to fully check everything. 我看不到memberblood_groups表来完全检查所有内容。

Check that 检查一下

1) All the foreign keys should match the type of primary key they match. 1)所有外键都应匹配它们匹配的主键的类型。 For example, if you use increments for PK make sure that FK has type of unsignedInteger 例如,如果对PK使用increments ,请确保FK具有unsignedInteger类型

2) Make sure the order is right. 2)确保订单正确。 You can't create the foreign key if you don't have a table. 如果没有表,则无法创建外键。 For the code, I see it should be something like 对于代码,我认为它应该类似于

Also, I found that in the hospitals table you have FK ( country_id ) to countries table and in the countries table you have FK ( hospital_id ) to hospitals table. 另外,我发现,在hospitals表,你有FK( country_id )到countries表,并在countries表,你有FK( hospital_id )到hospitals表。 You can't do like that, there should only be one of then (probably country_id ). 您不能那样做,只能是其中之一(可能是country_id )。 The reason why is because (I don't know which of those are created first, so let say it's hospitals ) when you create hospitals , you try to make FK to the table that doesn't exists. 原因是因为(我不知道首先创建哪个,所以说是hospitals )在创建hospitals ,您尝试将FK设置为​​不存在的表。

暂无
暂无

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

相关问题 一般错误:1005 无法创建表(错误号:150“外键约束格式不正确”) - General error: 1005 Can't create table (errno: 150 "Foreign key constraint is incorrectly formed") 一般错误:1005 无法创建表...(errno:150“外键约束格式不正确” - General error : 1005 Can't create table ... (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") Laravel 一般错误:1005 无法创建表`categories_products`(错误号:150“外键约束的格式不正确”) - Laravel General error: 1005 Can't create table `categories_products` (errno: 150 "Foreign key constraint is incorrectly formed") : 1005 无法创建表 `shop`.`role_user` (errno: 150 “外键约束格式不正确”)") - : 1005 Can't create table `shop`.`role_user` (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 我如何在 Laravel 中修复此错误”1005 无法创建表 `englishcollage`.`role_user`(错误号:150“外键约束的格式不正确” - how can i fix this eror in laravel" 1005 Can't create table `englishcollage`.`role_user` (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" 无法创建表`clothing`.`clothes`(错误号:150“外键约束形成不正确”)”)Laravel 7 - Can't create table `clothing`.`clothes` (errno: 150 "Foreign key constraint is incorrectly formed")") Laravel 7 (错误号:150“外键约束格式不正确”) - (errno: 150 “Foreign key constraint is incorrectly formed”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM