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
JavaScript
x
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.
JavaScript
$table->increments('id');
$table->unsignedInteger('user_id')->nullable();
$table->unsignedInteger('admin_id')->nullable();
Alternatively you can create a foreign key reference also.
JavaScript
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('admin_id')->references('id')->on('users');