您好,这是我的代码,但我收到此错误:
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();
});
}
谢谢
我一直在寻找它,但没有找到任何东西
问题解决了!
我只需删除
$table->id();
。
因为该表是相关的并且不需要 ID。