Skip to content
Advertisement

whats causing the migration to fail with error ” there can only be on auto increment column”

My table posts does not have any limits, i am letting Laravel choose the auto increment by default. It still fails with “there can only be on auto increment column”.

migration

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->id('user_id')->nullable();
        $table->id('admin_id')->nullable();
        $table->string('about')->nullable();
        $table->decimal('price')->nullable();
        $table->string('new_used')->nullable();
        $table->string('negotiable')->nullable();
        $table->dateTime('expire_date')->nullabe();

      
        $table->timestamps();
    });
}

Advertisement

Answer

You can’t have multiple auto incremented columns. Change the foreign keys type to unsignedInteger() and it should work. This is usually the way i create and id column plus two foreign keys.

$table->increments('id');
$table->unsignedInteger('user_id')->nullable();
$table->unsignedInteger('admin_id')->nullable();

Alternatively you can create a foreign key reference also.

$table->foreign('user_id')->references('id')->on('users');
$table->foreign('admin_id')->references('id')->on('users');
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement