【问题标题】:Cannot create multiple Primary Keys on Migration for LaravelLaravel 迁移时无法创建多个主键
【发布时间】:2020-08-29 15:42:27
【问题描述】:

我正在为我工​​作的企业的数据库进行迁移。

问题是同一个表有 4 个主键,但这个错误是由 Laravel 抛出的。

这就是我的迁移的样子:

Schema::create('ventas', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('empresa_id')->primary();
        $table->integer('moneda_id')->primary();
        $table->integer('user_id')->primary();

        $table->foreign('empresa_id')->references('id')->on('empresas')->onDelete('restrict');
        $table->foreign('moneda_id')->references('id')->on('monedas')->onDelete('restrict');

我该如何解决这个问题?

谢谢!

【问题讨论】:

  • 每个表只能有一个主键,但是有复合主键
  • 如何创建复合 PK?
  • 你可以看看stackoverflow.com/questions/31415213/… m 但我个人觉得 laralveland 雄辩可怕
  • “三个单独的键”(可能是一个主键 + 两个唯一约束)和具有三列的“单个复合键”之间存在差异。你需要哪一个?
  • 您似乎也有一个自动增量 id 字段。如果你有这样一个字段,那么你应该把它作为主键,否则我不明白它为什么在那里。

标签: mysql laravel


【解决方案1】:

试试这个

    public function up()
        {
            Schema::create('ventas', function (Blueprint $table) {
                $table->id();
                $table->foreignId('empresa_id')->nullable()->constrained()->onDelete('restrict');
                $table->foreignId('moneda_id')->nullable()->constrained()->onDelete('restrict');
                $table->foreignId('user_id')->nullable()->constrained()->onDelete('restrict');
                $table->timestamps();
            });
        }

【讨论】:

猜你喜欢
  • 2014-05-30
  • 1970-01-01
  • 1970-01-01
  • 2018-03-06
  • 2016-11-20
  • 1970-01-01
  • 2013-07-26
  • 2020-04-13
  • 2017-08-16
相关资源
最近更新 更多