laravel migration use auto increment when its not primary

2.2k Views Asked by At

I'm trying to create a table with Laravel migrations but I'm having some trouble. I just need to create a table with a primary pair ('user_id' and 'media_id'), being 'inc' an auto increment. I can do it in MySQL, but I can't manage to do it with Laravel Migrations since increments() also set the field as primary. error i get

 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

thats i hae done so far

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

2

There are 2 best solutions below

2
On

I think you need this:

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

and put this in the model:

protected $primaryKey = 'inc';
0
On

I found solution to problem which is ti define that column inc (which auto increments) after creating schema.

That how it should be

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