我正在尝试使用Laravel迁移创建一个表,但我遇到了一些麻烦。我只需要创建一个带有主要对的表('user_id'和'media_id'),'inc'是一个自动增量。我可以在MySQL中完成它,但是我无法通过Laravel Migrations来完成它,因为incrementments()也将字段设置为主要字段。错误我得到
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
那是我到目前为止所做的那些
public function up()
{
Schema::create('tagtables', function (Blueprint $table) {
$table->integer('media_id');
$table->integer('user_id');
$table->boolean('approved')->default(false);
$table->increments('inc')->unsigned();
$table->timestamps();
$table->dropPrimary('tagtables_inc_primary');
$table->primary(array('user_id','media_id'));
// $table->foreign('media_id')->references('id')->on('media')->onUpdate('cascade')->onDelete('cascade');
// $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
// Schema::table('tagtables', function (Blueprint $table) {
// //$table->increments('id');
// $table->primary(array('user_id','media_id'));
// $table->foreign('media_id')->references('id')->on('media')->onUpdate('cascade')->onDelete('cascade');
// $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
// });
}
答案 0 :(得分:0)
我认为你需要这个:
public function up()
{
Schema::create('tagtables', function (Blueprint $table) {
$table->increments('inc');
$table->integer('media_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->boolean('approved')->default(false);
$table->timestamps();
$table->index(['user_id', 'media_id']);
$table->foreign('media_id')->references('id')->on('media')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
}
并将其放入模型中:
protected $primaryKey = 'inc';
答案 1 :(得分:0)
我找到了解决问题的方法,即在创建模式后定义列inc(自动递增)。
应该如何
public function up()
{
Schema::create('tagtables', function (Blueprint $table) {
$table->integer('media_id');
$table->integer('user_id');
$table->boolean('approved')->default(false);
// $table->increments('inc');
$table->timestamps();
// $table->dropPrimary( 'tagtables_inc_primary' );
$table->primary(array('user_id','media_id'));
// $table->foreign('media_id')->references('id')->on('media')->onUpdate('cascade')->onDelete('cascade');
// $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
DB::statement('ALTER Table tagtables add id INTEGER NOT NULL UNIQUE AUTO_INCREMENT;');
// Schema::table('tagtables', function (Blueprint $table) {
// //$table->increments('id');
// $table->primary(array('user_id','media_id'));
// $table->foreign('media_id')->references('id')->on('media')->onUpdate('cascade')->onDelete('cascade');
// $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
// });
}
chema