简体   繁体   English

SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4

[英]SQLSTATE[HY000]: General error: 1005 Can't create table - Laravel 4

I get this error when I do php artisan migrate.我在执行 php artisan 迁移时收到此错误。 Is there something wrong in my migration files?我的迁移文件有问题吗? Or is it possible my models are wrong coded?还是我的模型编码错误? But the migrations should work even there is something wrong in the models?但是即使模型中有问题,迁移也应该起作用?

[Exception]                                                                  
  SQLSTATE[HY000]: General error: 1005 Can't create table 'festival_aid.#sql-  
  16643_2033' (errno: 150) (SQL: alter table `gigs` add constraint gigs_band_  
  id_foreign foreign key (`band_id`) references `bands` (`band_id`) on delete  
   cascade) (Bindings: array (                                                 
  ))

[PDOException]                                                               
  SQLSTATE[HY000]: General error: 1005 Can't create table 'festival_aid.#sql-  
  16643_2033' (errno: 150)

gigs migration演出迁移

public function up()
    {
        Schema::create('gigs', function($table)
        {
            $table->increments('gig_id');

            $table->dateTime('gig_startdate');

            $table->integer('band_id')->unsigned();
            $table->integer('stage_id')->unsigned();

            $table->foreign('band_id')
                ->references('band_id')->on('bands')
                ->onDelete('cascade');

            $table->foreign('stage_id')
                ->references('stage_id')->on('stages')
                ->onDelete('cascade');
        });

    public function down()
    {
        Schema::table('gigs', function($table)
        {
            Schema::drop('gigs');
            $table->dropForeign('gigs_band_id_foreign');
            $table->dropForeign('gigs_stage_id_foreign');
        });
    }

bands migration带迁移

public function up()
    {
        Schema::create('bands', function($table)
        {
            $table->increments('band_id');

            $table->string('band_name');
            $table->text('band_members');
            $table->string('band_genre');
            $table->dateTime('band_startdate');
        });
    }

    public function down()
    {
        Schema::table('bands', function(Blueprint $table)
        {
            Schema::drop('bands');
        });
    }

Model Band模型乐队

<?php

class Band extends Eloquent {

    protected $primaryKey = 'band_id';

    public function gig()
    {
        return $this->hasOne('Gig', 'band_id', 'band_id');
    }
}

Model Gig模型演出

<?php

class Gig extends Eloquent {
    protected $primaryKey = 'gig_id';

    public function gig()
    {
        return $this->belongsTo('Band', 'band_id', 'band_id');
    }

    public function stage()
    {
        return $this->belongsTo('Stage', 'stage_id', 'stage_id');
    }
}

You must first create the table, then create the foreign keys:您必须首先创建表,然后创建外键:

Schema::create('gigs', function($table)
{
    $table->increments('gig_id');

    $table->dateTime('gig_startdate');

    $table->integer('band_id')->unsigned();
    $table->integer('stage_id')->unsigned();
});

Schema::table('gigs', function($table)
{
    $table->foreign('band_id')
        ->references('band_id')->on('bands')
        ->onDelete('cascade');

    $table->foreign('stage_id')
        ->references('stage_id')->on('stages')
        ->onDelete('cascade');
});

And your bands table should migrate first, since the gigs is referencing it.并且您的bands表应该首先迁移,因为gigs正在引用它。

While this doesn't apply to OP, others might have this issue:虽然这不适用于 OP,但其他人可能会遇到此问题:

From the bottom of the Laravel Schema docs :Laravel Schema 文档的底部:

Note: When creating a foreign key that references an incrementing integer, remember to always make the foreign key column unsigned.注意:创建引用递增整数的外键时,请记住始终使外键列无符号。

You can do this via $table->integer('user_id')->unsigned();你可以通过$table->integer('user_id')->unsigned(); when creating your table in the migration file.在迁移文件中创建表时。

Took me a few minutes to realize what was happening.我花了几分钟才意识到发生了什么。

对于那些其他答案没有帮助的人,当您尝试对不可为空的列使用'SET_NULL'操作时,也会引发相同的错误。

As said by Andrew by making the reference on the table as this:正如Andrew所说,在桌子上这样引用:

$table->integer('user_id')->unsigned();

It should work.它应该工作。

Nothing of the above worked for me !以上没有对我有用! But this did : I just changed integer to unsignedInteger但是这样做了:我只是将整数更改为 unsignedInteger

$table->unsignedBigInteger('user_id')->unsigned();

for those who are still having this issue, make sure the field you setting as foreign key is a primary key or should be unique since a foreign key can only be a primary or unique field.对于那些仍然遇到此问题的人,请确保您设置为外键的字段是主键或应该是唯一的,因为外键只能是主字段或唯一字段。 see sample below请参阅下面的示例

Schema::create('users', function (Blueprint $table) {
  $table->increments('id');
  $table->string('username',25)->unique();
 });

Schema::create('another_table', function (Blueprint $table) {
   $table->increments('id');
   $table->string('name', 25);
   $table->foreign('name')->references('username')->on('users')
 });
        $table->integer('band_id')->unsigned();
        $table->integer('stage_id')->unsigned();

In laravel 5.8, the users_table uses bigIncrements('id') data type for the primary key.在 laravel 5.8 中, users_table 使用 bigIncrements('id') 数据类型作为主键。 So that when you want to refer a foreign key constraint your band_id,stage_id column needs to be unsignedBigInteger('stage_id') and band_id type.因此,当您想引用外键约束时,您的 band_id,stage_id 列需要是 unsignedBigInteger('stage_id') 和 band_id 类型。

The manager also tested this way.经理也这样测试过。

I had a similar problem.我有一个类似的问题。 You must use bigInteger to solve this problem:您必须使用 bigInteger 来解决此问题:

$table->bigInteger('user_id')->unsigned();

This seems to be a general foreign key issue error.这似乎是一般的外键问题错误。 For me I got it when I switched the arguments in the references and on methods.对我来说,当我切换references和方法on的参数时,我得到了它。 I had:我有:

$table->foreign('user_id')->references('users')->on('id');

instead of:代替:

$table->foreign('user_id')->references('id')->on('users');

I just fixed this problem by making pointing to Laravel that the field is unique using unique() .我刚刚通过使用unique()指向 Laravel 该字段是唯一的来解决这个问题。

Example:例子:

Schema::create('branches', function (Blueprint $table) {
            $table->increments('id');
            /* This fields serves as foriegn key on functions 
            table and it must follow the database **key rules**
            by being unique */
            $table->string('branch_code')->unique();
            $table->string('branch_name');
            $table->string('branch_address');
            $table->timestamps();
        });

Functions table:功能表:

    Schema::create('functions', function (Blueprint $table) {
        $table->increments('id');
        $table->string('branch_code');
        $table->string('function');
        $table->timestamps();
        //Reference to branch_code on branches table
        $table->foreign('branch_code')->references('branch_code')->on('branches');

    });

我在 config/database 'engine' => 'InnoDB',更改为'engine' => null,并且可以正常工作

You can use您可以使用

$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('users')->on('id');

BigInt has datatype of bigint(20) but integer has datatype of int(10). BigInt 的数据类型为 bigint(20),而整数的数据类型为 int(10)。 Though both of those are unsigned integer, 'Length/Values' does not match.虽然这两个都是无符号整数,但“长度/值”不匹配。

For Laravel 6.X use this format.对于 Laravel 6.X,使用这种格式。

$table->unsignedBigInteger('dest_id')->unsigned();

$table->foreign('dest_id')->references('id')->on('destinations')->onDelete('cascade');

Changing from:变化自:

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

To:至:

$table->unsignedBigInteger('user_id')->unique();

worked for me为我工作

The foreign key must use either unsignedInterger or unsignedBigInterger外键必须使用 unsignedInterger 或 unsignedBigInterger

exam:考试:

public function up()
{
    Schema::create('likes', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('user_id');
        $table->unsignedInteger('post_id');
        $table->string('post_type');
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
        $table->timestamps();
    });
}

You may also specify the desired action for the "on delete" and "on update" properties of the constraint:您还可以为约束的“on delete”和“on update”属性指定所需的操作:

$table->foreignId('user_id')
  ->constrained()
  ->onUpdate('cascade')
  ->onDelete('cascade');

I think you should checkout the referenced tables , for example you could get such errors if you pass the values .我认为您应该检查引用的表,例如,如果您传递值,您可能会收到此类错误。 This could be the reason $table->foreign('course_id')->references('course_id')->on('ptable'); which is wrong but这可能是$table->foreign('course_id')->references('course_id')->on('ptable'); which is wrong but $table->foreign('course_id')->references('course_id')->on('ptable'); which is wrong but $table->foreign('course_id')->references('level_id')->on('ptable');` is correct $table->foreign('course_id')->references('course_id')->on('ptable'); which is wrong but $table->foreign('course_id')->references('level_id')->on('ptable');` 是正确的

暂无
暂无

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

相关问题 SQLSTATE[HY000]:一般错误:1005 无法创建表 Laravel 8 - SQLSTATE[HY000]: General error: 1005 Can't create table Laravel 8 Laravel 5.4:SQLSTATE[HY000]:一般错误:1005 无法创建表“外键约束格式不正确” - Laravel 5.4: SQLSTATE[HY000]: General error: 1005 Can't create table "Foreign key constraint is incorrectly formed" SQLSTATE[HY000]:一般错误:无法创建表 - SQLSTATE[HY000]: General error:Can't create table 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 工匠迁移结果:SQLSTATE [HY000]:一般错误:LUMEN / LARAVEL上为1005 - artisan migrate results: SQLSTATE[HY000]: General error: 1005 on LUMEN / LARAVEL 找不到表(SQLSTATE [HY000]:常规错误:1无此类表:用户) - Can't find table (SQLSTATE[HY000]: General error: 1 no such table: users) 错误1005(HY000):无法创建表“ db.POSTS”(错误号:150) - ERROR 1005 (HY000): Can't create table 'db.POSTS' (errno: 150) SQLSTATE [HY000]:一般错误:Laravel发生2053错误 - SQLSTATE[HY000]: General error: 2053 error occurs at Laravel 更新表格错误? (SQLSTATE [HY000]:一般错误) - Updating table error? (SQLSTATE[HY000]: General error)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM