-3

I'm doing migrations for the DB of the enterprise I work for.

The issue is that the same table has 4 primary keys but this error is being throw by Laravel.

This is how my migrations look like:

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');

How can I solve this?

Thanks!

Rodri6uez
  • 399
  • 1
  • 5
  • 14
  • 1
    every table can have only **one** primary key, but there are composite primary keays – nbk Aug 29 '20 at 15:53
  • How can I create a Composite PK? – Rodri6uez Aug 29 '20 at 15:55
  • you can have alook at https://stackoverflow.com/questions/31415213/how-i-can-put-composite-keys-in-models-in-laravel-5 m but i personally find laralveland eloquent horrible – nbk Aug 29 '20 at 15:57
  • 1
    There's a difference between "three separate keys" (maybe one primary + two unique constraints) and a "single composite key" with three columns. Which one do you need? – The Impaler Aug 29 '20 at 16:02
  • 1
    You also seem to have an auto increment id field. If you have such a field, then you should make that the primary key, otherwise I would not understand why it's there. – Shadow Aug 29 '20 at 16:16
  • I already tried the composite PK but it did not work. My task is to create this table with 4 PK but it seems that is not possible. I asked if I can have 3 Uniques and 1 PK but my supervisor told me: "NO". – Rodri6uez Aug 29 '20 at 16:19

1 Answers1

0

try this

    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();
            });
        }
Abdulmajeed
  • 552
  • 7
  • 8