Syntax error or access violation: 1068 Multiple primary key defined

huangapple go评论63阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月8日 19:23:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672379.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定