Eloquent - 无法添加外键约束

时间:2017-04-25 15:01:30

标签: sql laravel eloquent

我猜这个常见问题,但我无法解决它,尽管我在网上找到了相关信息。

我有一系列hasMany关系:

  • 用户 有许多 客户有多个合同有多个材料。< / LI>

我尽我所能,但我发现自己遇到了这个错误:

  

[照亮\数据库\ QueryException]
  SQLSTATE [HY000]:常规错误:1215无法添加外键约束   (SQL:create table clientsid int unsigned not null,user_id   int unsigne d null,...)默认字符集utf8mb4 collat​​e
  utf8mb4_unicode_ci engine = InnoDB)

     

SQLSTATE [HY000]:常规错误:1215无法添加外键   约束

迁移:

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->engine = 'InnoDB';

            // Keys

            $table->increments('id');

            // Other

            ...
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('clients');
        Schema::drop('users');
        Schema::enableForeignKeyConstraints();
    }
}

class CreateClientsTable extends Migration
{
    public function up()
    {
        Schema::create('clients', function (Blueprint $table) {
            $table->engine = 'InnoDB';

            // keys

            $table->unsignedInteger('id')->unique();
            $table->primary('id');

            $table->unsignedInteger('user_id')
                  ->nullable();
            $table->foreign('user_id')->references('id')->on('users')
                  ->onDelete('cascade');

            // others
            ...
        });
    }

    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('contracts');
        Schema::drop('clients');
        Schema::enableForeignKeyConstraints();
    }
}

class CreateContractsTable extends Migration
{
    public function up()
    {
        Schema::create('contracts', function (Blueprint $table) {
            $table->engine = 'InnoDB';

            // Keys

            $table->increments('id');
            $table->string('contract_number')->unique();

            $table->unsignedInteger('client_id');
            $table->foreign('client_id')->references('id')->on('clients')
                  ->onDelete('cascade');


            // Others

            ...
        });
    }

    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('materials');
        Schema::drop('contracts');
        Schema::enableForeignKeyConstraints();
    }
}

class CreateMaterialsTable extends Migration
{
    public function up()
    {
        Schema::create('materials', function (Blueprint $table) {
            $table->engine = 'InnoDB';

            // Keys

            $table->increments('id');

            $table->string('contract_number')->unique();

            $table->unsignedInteger('contract_id');
            $table->foreign('contract_id')->references('id')->on('contracts')
                  ->onDelete('cascade');

            // Others

            ...
        });
    }

    public function down()
    {
        Schema::drop('materials');
    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您应该将->unsigned()添加到您拥有的所有关键列。 请使用此代替unsignedInteger(),并将类型设置为integer()

答案 1 :(得分:0)

您确定您的迁移按正确顺序运行吗?它们应该按照您在示例代码中提供的顺序运行。我似乎无法使用您的确切示例迁移重新创建我的错误。

订单由迁移文件名开头的日期决定。或者,您可以在同一个迁移中创建所有这些表,以强制顺序。