Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any advantages of adding an id column to a pivot table in Laravel?

Tags:

Is there any advantages of having an id column in a pivot table (many to many relationship) in Laravel (i'm using version 5.1)?

With an id

        $table->increments('id');

        $table->integer('appointment_id')->unsigned();
        $table->foreign('appointment_id')->references('id')->on('appointments')->onDelete('cascade');

        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');

        $table->timestamps();

Without an id

        $table->integer('appointment_id')->unsigned();
        $table->foreign('appointment_id')->references('id')->on('appointments')->onDelete('cascade');

        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');

        $table->timestamps();
like image 923
user3489502 Avatar asked Dec 14 '15 19:12

user3489502


1 Answers

In general, the answer is no, provided that Laravel's Eloquent models are managing the relationship.

If, however, you need to access the tables from outside of Eloquent models (say, from another application or in the distant future when you rewrite your application to use the next big framework), an ID will come in handy.

like image 200
Kryten Avatar answered Oct 01 '22 06:10

Kryten