【问题标题】:Laravel 5.8 : Always give multiple primary key syntax or access violation errorLaravel 5.8:总是给出多个主键语法或访问冲突错误
【发布时间】:2020-10-22 18:16:34
【问题描述】:

我用复合主键创建了迁移文件,但总是报错

语法错误或访问冲突:1068 多个主键定义(sql : alter table 'table_currency' 添加主键 table_currency_code_user_id_primary('code', 'user_id'))

 Schema::create('table_currency', function (Blueprint $table) {
        $table->string('code', 3);
        $table->bigIncrements('user_id');
        $table->string('default', 3);
        $table->enum('is_active', ['0','1'])->default('0')->comment('0: Inactive, 1: Active');
        $table->timestamps();
        $table->primary(['code', 'user_id']);
    });

我不明白为什么会出现这个错误?提前谢谢。

【问题讨论】:

    标签: eloquent laravel-5.8


    【解决方案1】:

    如果您想在['user_id', 'code'] 上创建复合主键,您需要删除附加在increments 列上的主键。您可以使用以下代码执行此操作:

     Schema::create('table_currency', function (Blueprint $table) {
        $table->bigIncrements('user_id'); // Has a primary key
        $table->dropPrimary('table_currency_user_id_primary'); // Remove the primary key from the increments column
        $table->primary(['code', 'user_id']); // Set your composite primary key
        $table->string('code', 3);
        $table->string('default', 3);
        $table->enum('is_active', ['0','1'])->default('0')->comment('0: Inactive, 1: Active');
        $table->timestamps();
        });
    

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 2020-08-17
      • 2020-05-03
      • 2019-02-10
      • 2017-04-16
      • 2021-12-06
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多