Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mithridates's avatar

defining primary keys in migration

Hi, I have implemented a table for letting twitter like following users.
Here is the migration:

    Schema::create('follows', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('follower_id')->unsigned();
            $table->integer('followed_id')->unsigned();
            $table->foreign('follower_id')->references('id')->on('users');
            $table->foreign('followed_id')->references('id')->on('users');
            $table->primary(array('follower_id', 'followed_id'));
            $table->timestamps();
        });

but I'm getting error from this line

$table->primary(array('follower_id', 'followed_id'));

*The error*
Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table `follows` add primary key follows_follower_id_followed_id_primary(`follower_id`,
  `followed_id`))

what I wanna is just let a combination of two user ids be seen just once
(user ids 3-4 and 4-3 are allowed in table simultaneously but 3-4, 3-4 not)
I hope I could get the meaning.

0 likes
6 replies
bobbybouwmann's avatar

You can't do that because the id field here is already a primary key, as stated in the documentation

$table->increments('id');   Incrementing ID to the table (primary key)

If you remove the id field from your table you should be fine ;)

1 like
paradox's avatar

yep as @bobbybouwmann said, just change

$table->increments('id');

to

$table->integer('id')->unsigned();

or skip it completely since you probably won't use that field

1 like
Mithridates's avatar

But what I know from traditional relational databases, every table should have a primary id value. what about that?

willvincent's avatar
Level 54

You don't necessarily need a primary key on every table.. This table looks like it's just a pivot table to me, to allow a many to many relationship between users. Those usually don't have a primary key, at least not a stand-alone primary key value. In this case, the combination of follower and followed would be the unique identifier, and you could make that a compound primary key, but you certainly wouldn't need to.

Since this appears to just be a pivot table though, I assume in your User model you're defining that a user has many users (as followers and as followed)?

Something like this:

public function follows()
{
  return $this->belongsToMany('User', 'follows', 'follower_id', 'followed_id');
}

public function followers()
{
  return $this->belongsToMany('User', 'follows', 'followed_id', 'follower_id');
}

Then to get the followers of the current user you would say:

$followers = Auth::user()->followers;

And people they are following:

$following = Auth::user()->follows;
bobbybouwmann's avatar

In this case you are using a pivot table. This table only connects two entities together to create a many-to-many relation. In this case you don't need a primary key at all!

1 like

Please or to participate in this conversation.