id
をオートインクリメント
id
、user_id
、type
を複合主キーに設定したい。
class CreateFoosTable extends Migration
{
public function up()
{
Schema::create('foos', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->tinyInteger('type');
$table->text('text');
$table->timestamps();
$table->primary(['id', 'user_id', 'type']);
});
}
}
マイグレーションを実行しようとするとエラーが出る
Syntax error or access violation: 1068 Multiple primary key defined
すでにプライマリーキーがすでに設定されているというエラー
$table->increments('id');
で主キーを設定して
$table->primary(['id', 'user_id', 'type']);
で再度複合主キーを設定しようとしている?
そう思ったので、id
を一度integer
に設定して主キーを設定した後にincrements
に変更してみた。
class CreateFoosTable extends Migration
{
public function up()
{
Schema::create('foos', function (Blueprint $table) {
$table->integer('id');
$table->integer('user_id');
$table->tinyInteger('type');
$table->text('text');
$table->timestamps();
$table->primary(['id', 'user_id', 'type']);
});
Schema::table('foos', function (Blueprint $table) {
$table->increments('id')->change();
});
}
}
requires Doctrine DBAL; install "doctrine/dbal".
doctrine/dbal
を追加しろと言われたので追加
カラム変更
動作要件
カラムを変更する前に、composer.jsonファイルでdoctrine/dbalを確実に追加してください。Doctrine DBALライブラリーは現在のカラムの状態を決め、指定されたカラムに対する修正を行うSQLクエリを生成するために、使用しています。
$composer require doctrine/dbal
できた。