遷移:無法添加外鍵約束 - Migration: Cannot add foreign key constraint

問題:

I'm trying to create foreign keys in Laravel however when I migrate my table using artisan i am thrown the following error:我正在嘗試在 Laravel 中創建外鍵,但是當我使用artisan遷移我的表時,我拋出了以下錯誤:

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `priorities` add constraint priorities_user_id_foreign foreign 
key (`user_id`) references `users` (`id`))     

My migration code is as so:我的遷移代碼如下:

priorities migration file優先級遷移文件

public function up()
{
    //
    Schema::create('priorities', function($table) {
        $table->increments('id', true);
        $table->integer('user_id');
        $table->foreign('user_id')->references('id')->on('users');
        $table->string('priority_name');
        $table->smallInteger('rank');
        $table->text('class');
        $table->timestamps('timecreated');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    //
    Schema::drop('priorities');
}

users migration file用戶遷移文件

public function up()
{
    //
    Schema::table('users', function($table)
    {
    $table->create();
    $table->increments('id');
    $table->string('email');
    $table->string('first_name');
    $table->string('password');
    $table->string('email_code');
    $table->string('time_created');
    $table->string('ip');
    $table->string('confirmed');
    $table->string('user_role');
    $table->string('salt');
    $table->string('last_login');

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

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    //
        Schemea::drop('users');
}

Any ideas as to what I've done wrong, I want to get this right now, as I've got a lot of tables I need to create eg Users, Clients, Projects, Tasks, Statuses, Priorities, Types, Teams.關於我做錯了什麼的任何想法,我想現在就得到這個,因爲我有很多表需要創建,例如用戶、客戶、項目、任務、狀態、優先級、類型、團隊。 Ideally I want to create tables which hold this data with the foreign keys, i..e clients_project and project_tasks etc.理想情況下,我想創建使用外鍵保存這些數據的表,即clients_projectproject_tasks等。

Hope someone can help me to get started.希望有人可以幫助我開始。


解決方案:

參考一: https://stackoom.com/question/1WtR0
參考二: Migration: Cannot add foreign key constraint
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章