繁体   English   中英

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

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

我在执行 php artisan 迁移时收到此错误。 我的迁移文件有问题吗? 还是我的模型编码错误? 但是即使模型中有问题,迁移也应该起作用?

[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)

演出迁移

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');
        });
    }

带迁移

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');
        });
    }

模型乐队

<?php

class Band extends Eloquent {

    protected $primaryKey = 'band_id';

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

模型演出

<?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');
    }
}

您必须首先创建表,然后创建外键:

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');
});

并且您的bands表应该首先迁移,因为gigs正在引用它。

虽然这不适用于 OP,但其他人可能会遇到此问题:

Laravel Schema 文档的底部:

注意:创建引用递增整数的外键时,请记住始终使外键列无符号。

你可以通过$table->integer('user_id')->unsigned(); 在迁移文件中创建表时。

我花了几分钟才意识到发生了什么。

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

正如Andrew所说,在桌子上这样引用:

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

它应该工作。

以上没有对我有用! 但是这样做了:我只是将整数更改为 unsignedInteger

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

对于那些仍然遇到此问题的人,请确保您设置为外键的字段是主键或应该是唯一的,因为外键只能是主字段或唯一字段。 请参阅下面的示例

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();

在 laravel 5.8 中, users_table 使用 bigIncrements('id') 数据类型作为主键。 因此,当您想引用外键约束时,您的 band_id,stage_id 列需要是 unsignedBigInteger('stage_id') 和 band_id 类型。

经理也这样测试过。

我有一个类似的问题。 您必须使用 bigInteger 来解决此问题:

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

这似乎是一般的外键问题错误。 对我来说,当我切换references和方法on的参数时,我得到了它。 我有:

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

代替:

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

我刚刚通过使用unique()指向 Laravel 该字段是唯一的来解决这个问题。

例子:

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();
        });

功能表:

    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,并且可以正常工作

您可以使用

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

BigInt 的数据类型为 bigint(20),而整数的数据类型为 int(10)。 虽然这两个都是无符号整数,但“长度/值”不匹配。

对于 Laravel 6.X,使用这种格式。

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

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

变化自:

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

至:

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

为我工作

外键必须使用 unsignedInterger 或 unsignedBigInterger

考试:

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();
    });
}

您还可以为约束的“on delete”和“on update”属性指定所需的操作:

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

我认为您应该检查引用的表,例如,如果您传递值,您可能会收到此类错误。 这可能是$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');` 是正确的

暂无
暂无

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

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