2016-08-29 6 views
0

以下の初期移行して、データベース内のテーブルがあります:トラブル(コンポジット)キー

public function up() 
    { 
     Schema::create('products_markets', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->integer('product_id')->unsigned(); 
      $table->foreign('product_id')->references('id')->on('products'); 
      $table->integer('country_code_id')->unsigned(); 
      $table->timestamp('created_at')->useCurrent(); 
      $table->timestamp('updated_at')->useCurrent(); 
     }); 

    $sql = 'ALTER TABLE `products_markets` ADD UNIQUE `unique_index`(`product_id`, `country_code_id`)'; 
    DB::connection()->getPdo()->exec($sql); 
} 

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

そして、すべてが正常に働いていました。

public function up() 
    { 
     Schema::table('products_markets', function (Blueprint $table) { 
      DB::unprepared("ALTER TABLE products_markets ADD INDEX country_code_id (country_code_id)"); 
      DB::unprepared('ALTER TABLE products_markets MODIFY id INT NOT NULL'); 
      DB::unprepared('ALTER TABLE products_markets DROP PRIMARY KEY'); 
      DB::unprepared('ALTER TABLE products_markets DROP COLUMN id'); 
      DB::unprepared('ALTER TABLE products_markets ADD PRIMARY KEY (country_code_id, product_id)'); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::table('products_markets', function (Blueprint $table) { 
      DB::unprepared('DROP INDEX country_code_id ON products_markets'); 
      DB::unprepared('ALTER TABLE products_markets DROP PRIMARY KEY'); 
      $table->increments('id'); 
     }); 
    } 

このコードは、ローカルでいくつかのマシン上で当社のサーバーと上で動作しますが:しかし、それは新しい移行が行われたid自動インクリメントの主キーを持っている意味がありませんでしたということrealizedだったら

ProductTest :: test_scopeMarket_only_returns_products_for_user_country を照らし\データベース\のQueryException:SQLSTATE [42S22]:カラム見つかりません:他マシンがテスト中にエラーを投げているように見えます1054不明列 'ID' の 'where句'(SQL:idがnull products_marketsが= 121 country_code_idを設定、更新、updated_at = 2016年8月29日夜05時05分21秒)

試験で次の行に質問のエラーがあります。

私たちは作者dumpautoload、再マイグレーション、キャッシュクリアを持っています。しかし、何も助けてくれないようです。これはDB::unpreparedALTERのテーブルを使用して行う必要がありますか?

+0

idが新しいプライマリキー( 'product_id'、' country_code_id')に使用されているすべてのクエリを書き換える必要があります –

+0

私はidがどんなクエリでも使われていないことを詳しく述べるべきだと思います。 –

+0

テストコードのどこかで使用されているようです。 –

答えて

1

答えはeloquent does not support composite primary keysです。

あなたが実行している場合:

$products[0]->markets()->update(['country_code_id' => 121]);

それが実行されます。しかし、あなたが実行した場合:

$products[0]->markets()->get()->update(['country_code_id' => 121]);

、それは失敗しますので、最初の()のモデルを返し、デフォルトのモデルのPrimaryKeyはidで、複合primaryKey sは雄弁では処理されません。

+0

悲しいかな、サードパーティのソフトウェアがもう1つのケースに入り込んでいます。 –

関連する問題