Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

4ever_bad's avatar

Duplicate key on write or update - Laravel 7

Hi. I'm tried to migrate my php files and an error has appear with table "topic".

Error:

SQLSTATE[HY000]: General error: 1005 Can't create table portal.topic (errno: 121 "Duplicate key on write or update") (SQL: alter table topic add constraint category_id foreign key (category_id) references categories (id) on delete restrict on update restrict)

Migration:

   public function up()
  {
    Schema::create($this->tableName, function (Blueprint $table) {

        $table->engine = 'InnoDB';

        $table->bigIncrements('id');
        $table->string('name');
        $table->text('description');

        $table->unsignedBigInteger('category_id');
        $table->index(["category_id"], 'category_id');
        $table->foreign('category_id', 'category_id')
            ->references('id')->on('categories')
            ->onDelete('restrict')
            ->onUpdate('restrict');
        });
    }

I created migrations using MySQL Workbench and all tables and relations was really fine there.

What can solve my issue?

1 like
2 replies
4ever_bad's avatar

I have renamed the foreign keys and it works

1 like

Please or to participate in this conversation.