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 ;)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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.
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;
Please or to participate in this conversation.