语法错误或访问冲突:1068 定义了多个主键

问题描述 投票:0回答:1

您好,这是我的代码,但我收到此错误:

 SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table `wishlists` add primary key (`userId`, `productId`))

代码:

public function up()
    {
        Schema::create('wishlists', function (Blueprint $table) {
            $table->id();
//            Foreign key for Users table
            $table->foreignId('userId');
            $table->foreign('userId')->references('id')->on('users')->onDelete('cascade');

//            Foreign key for Products table
            $table->foreignId('productId');
            $table->foreign('productId')->references('id')->on('products')->onDelete('cascade');

            $table->primary(['userId', 'productId']);

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

谢谢

我一直在寻找它,但没有找到任何东西

php mysql laravel migration mysql-error-1068
1个回答
2
投票

问题解决了!

我只需删除

$table->id();
。 因为该表是相关的并且不需要 ID。

© www.soinside.com 2019 - 2024. All rights reserved.