8

私は現在、マスターサーバーと多くのクライアントで構成されるLaravel4プロジェクトに取り組んでいます。クライアントはデータを作成し、これをマスターサーバーに送信します。競合を回避するために、主キーとしてUUIDv4を使用しています。

ただし、サーバー上でデータが作成されたら、ユーザーがデータを識別しやすくするために、一意の自動インクリメント整数を割り当てたいと思います。item 5a8e896d-3ab4-48d2-9d39-faeb5227f012例:ユーザーについて話す代わりに、item #24567

アプリを管理しやすくするために、移行を使用しています。このテーブルの現在の移行は次のようになります。

public function up()
{
    Schema::table('items', function($table)
    {
        $table->create();
        $table->string('id')->primary(); //'id' For the purpose of keeping the ORM working, this field stores the UUID.
        $table->integer('number', true); //The human readable item number, the second parameter is true for auto-increment
        $table->text('otherdata');
        $table->timestamps();
    });
}

問題は、自動インクリメントを定義するときにLaravelが自動的に主キーを作成し、2つの主キーがあるために移行が失敗することです。

[Exception] SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined
  (SQL: alter table `items` add primary key items_id_primary(`id`)) (Bindings: array ())

Laravel 4の移行を使用して、主キーと個別の自動インクリメントフィールドを備えたテーブルを作成する方法はありますか?

4

4 に答える 4

2

問題が見つかりました。Laravelはauto_incrementフィールドごとに主キーを作成しているようです。パーツを削除primary keyすると、インデックスを提供するように求められたので->unique()、移行を要求しましたが、これも機能しませんでした。に変更return ' auto_increment primary key';return ' auto_increment unique'ます; 私の問題は解決しましたが、コアでハッキングされていますが、これはもちろん悪い習慣です。

/**
 * Get the SQL for an auto-increment column modifier.
 *
 * @param  Illuminate\Database\Schema\Blueprint  $blueprint
 * @param  Illuminate\Support\Fluent  $column
 * @return string|null
 */
protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
{
    if ($column->type == 'integer' and $column->autoIncrement)
    {
        return ' auto_increment unique'; //return ' auto_increment primary key';
    }
}
于 2013-03-24T10:31:02.577 に答える
1

トリックは、Schema::createの外に追加することです。

Schema::create('payments', function(Blueprint $table)
{
   $table->string('primaryKey', 30);
   $table->primary('primaryKey');
   //...
});
DB::statement('ALTER Table tableName add id INTEGER NOT NULL UNIQUE AUTO_INCREMENT;');

次に移行をやり直すと、キーはテーブルtableNameに名前IDで作成され、他のキーと同じようにアクセスできます。

于 2017-05-14T01:36:56.880 に答える
-1

auto_インクリメントを作成すると自動的に主キーになるため、コアファイルを変更せずに実行することはできないと思います。

Larvelフレームワーク開発でバグとして報告できればもっと良いです。チーム。

ここに 感謝します:)

于 2013-06-18T09:38:29.780 に答える
-2

はい、ItemsクラスのModel宣言でそれを行うことができます

class Items extends Eloquent {
   /**
 * Indicates if the IDs are auto-incrementing.
 *
 * @var bool
 */
public $incrementing = false;

}

これで、主キーは自動インクリメントフィールドではなくなりました。

于 2013-03-23T17:32:19.843 に答える