嗨,这是我的代码,但我收到这个错误:
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();
});
}
谢谢
4.我一直在找它,但什么也没找到
1条答案
按热度按时间mklgxw1f1#
问题解决了!
我只需要移除$table-〉id();.因为该表是相关的,不需要ID。