1

我正在尝试使用 Laravel 迁移创建一个表,但我遇到了一些麻烦。我只需要创建一个带有主对('user_id' 和'media_id')的表,'inc' 是一个自动增量。我可以在 MySQL 中做到这一点,但我无法用 Laravel 迁移做到这一点,因为 increments() 也将该字段设置为主要字段。我得到的错误

 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');
            
        // });
    }

4

2 回答 2

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';
于 2017-05-15T22:17:16.013 回答
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');
            
        // });
    }

化学

于 2017-05-15T22:47:58.103 回答