2017-02-09 2 views
0

マイグレーションをリフレッシュした後で、ユーザーの移行とアカウントのタイプユーザーの間で問題が発生する場合は、問題を把握できません。DBテーブルの移行をリフレッシュするときのエラー

エラー

[Illuminate\Database\QueryException] 
    SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'account_typ 
    es' already exists (SQL: create table `account_types` (`id` int unsigned no 
    t null auto_increment primary key, `name` varchar(50) not null, `created_at 
    ` timestamp null, `updated_at` timestamp null) default character set utf8mb 
    4 collate utf8mb4_unicode_ci) 



    [PDOException] 
    SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'account_typ 
    es' already exists 

マイ移行

アカウントの種類

public function up() 
    { 
     Schema::create('account_types', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name', 50); 
      $table->timestamps(); 
     }); 
    } 


    public function down() 
    { 
     Schema::dropIfExists('account_types'); 
    } 

ユーザー

public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name'); 
      $table->string('surname', 20); 
      $table->string('email')->unique(); 
      $table->string('password'); 
      $table->string('mobilephone', 9); 
      $table->rememberToken(); 
      $table->timestamps(); 
     }); 


    } 
    public function down() 
    { 
     Schema::dropIfExists('users'); 
    } 

ユーザーとアカウントの種類の関係については、ここに問題があると思われますが、移行コードに何が間違っているのかわかりません。

ユーザーとアカウントの種類の関係

public function up() 
    { 
     Schema::table('users', function($table) { 
      $table->integer('account_type_id')->unsigned(); 
      $table->foreign('account_type_id') 
       ->references('id')->on('account_types'); 
     }); 
    } 


    public function down() 
    { 
     Schema::table('users', function (Blueprint $table) { 
      $table->dropForeign('users_account_type_id_foreign'); 
      $table->dropColumn('account_type_id'); 
     }); 
    } 
+0

移行の実行順序をどのようにチェックアウトしたか。 'php artisan migrate:rollback'を実行すると、あなたの' migrations'テーブルの一番下で起動し、それをバックアップします – Spholt

答えて

0

私はそこの前に多くの時間を有していたと同じように移行スクリプトを登録する機会がなかったので、あなたが移行し、このエラーが発生したエラーを発生しました最後の移行。

すでに格納されているデータを気にしない場合は、データベース内のすべてのテーブルを手動で削除してください(移行テーブルも)。

テーブルにデータを保存する場合は、この移行テーブルを確認してバッチ列を確認する必要があります。 1にするすべてのエントリを保存し、その下の行とその下の行を0に設定します(おそらく削除することもできます)。実行php artisan migrate。 phpmyadminを使用している場合、これらの行をクエリで更新することができます。例:UPDATE migrations SET batch=0 WHERE migration LIKE "%create_users_table";

0

私はあなたがこれを試してみてくださいと思う:

が最初にデータベースに移行テーブルからaccount_typesユーザーを削除します。

移行を更新します。

これはあなたのために働くことを望みます!

関連する問題