我第一次創造了所有的表,而不用自己的外鍵,然後我添加了外鍵的每個表,從第一個表,我得到這個錯誤:不能添加外鍵約束Laravel 5.1
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `accounts` add constraint accounts_client_id_foreign foreign
key (`client_id`) references `clients` (`id`))
這裏的我的代碼:
public function up()
{
Schema::create('accounts', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->bigInteger('id');
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('clients');
$table->integer('emp_id')->unsigned();
$table->foreign('emp_id')->references('id')->on('employees');
$table->string('type');
$table->timestamps();
});
}
我嘗試沒有$表 - >引擎= 'InnoDB的';但同樣的錯誤
另外,我試圖分開外鍵:
Schema::table('accounts', function($table) {
$table->foreign('client_id')->references('id')->on('clients');
$table->foreign('emp_id')->references('id')->on('employees');
});
我得到這個錯誤:
Base table or view already exists: 1050 Table 'accounts' already exists
所以,當我刪除和重新遷移我得到的第一個錯誤
所以發生了什麼?
表中的數據不符合fk約束嗎?你可以嘗試向外鍵添加空值 –
我仍然沒有將數據添加到表 – Guest012393