英文:
Syntax error or access violation: 1068 Multiple primary key defined
问题
抱歉,你的要求是只返回翻译好的部分,不包括其他内容。以下是内容的中文翻译:
"Hi there this my code but I receive this error:"
"你好,这是我的代码,但我收到了以下错误:"
"SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table wishlists
add primary key (userId
, productId
))"
"SQLSTATE[42000]:语法错误或访问违规:1068 多个主键已定义(SQL:alter table wishlists
add primary key (userId
, productId
)"
"The code:"
"代码:"
"public function up()"
"public function up()"
"Schema::create('wishlists', function (Blueprint $table) {"
"Schema::create('wishlists',function(Blueprint $table) {"
"$table->id();"
"$table->id();"
"// Foreign key for Users table"
"// 用户表的外键"
"$table->foreignId('userId');"
"$table->foreignId('userId');"
"$table->foreign('userId')->references('id')->on('users')->onDelete('cascade');"
"$table->foreign('userId')->references('id')->on('users')->onDelete('cascade');"
"// Foreign key for Products table"
"// 产品表的外键"
"$table->foreignId('productId');"
"$table->foreignId('productId');"
"$table->foreign('productId')->references('id')->on('products')->onDelete('cascade');"
"$table->foreign('productId')->references('id')->on('products')->onDelete('cascade');"
"$table->primary(['userId', 'productId']);"
"$table->primary(['userId', 'productId']);"
"$table->timestamps();"
"$table->timestamps();"
"});"
"});"
"thanks"
"谢谢"
"I've been search about it but couldn't find anything"
"我已经搜索了一下,但没有找到相关信息"
英文:
Hi there this my code but I receive this error :
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table `wishlists` add primary key (`userId`, `productId`))
The code:
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();
});
}
thanks
I've been search about it but couldn't find anything
答案1
得分: 2
问题解决!
我只需删除 $table->id(); 。
因为表是相关的,不需要 ID。
英文:
Problem solved!
I just had to remove $table->id(); .
because the table is a relevant and don't need ID.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论