Laravel:外键错误

时间:2015-06-24 14:20:21

标签: eloquent laravel-5

我收到此错误:

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `offers` add constraint offers_client_id_foreign foreign key (`client_id`) references `clients` (`id`))

[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

创建表客户端

Schema::create('clients', function(Blueprint $table) {
    $table->increments('id')->unsigned();
    ...
});

创建表格提供

Schema::create('offers', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('client_id')->unsigned();
    ...
});

Schema::table('offers', function($table) {
    $table->foreign('client_id')
          ->references('id')
          ->on('clients');
});

模特报价

class Offer extends Model {
    /**
     * Relations
     * An offer belongs to client
     */

    public function client() {
        return $this->belongsTo('App\Client');
    }
}

模型客户端

class Client extends Model {
    /**
     * Relations
     * A client can have many offers
     */

    public function offers() {
        return $this->hasMany('App\Offer');
    }

}

我在表格提供之前创建表格客户端。 什么想法可能是错的?提前谢谢。

2 个答案:

答案 0 :(得分:1)

尝试删除未签名的客户主键 id

searchBar

增量方法已经意味着无符号整数。

答案 1 :(得分:0)

我道歉,我正在查看订单的播种程序。最后,我确实在客户表之前创建了要约表 - 在迁移中。

现在正在运作。

相关问题