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

sh1r3f's avatar

Duplicate entry while using updateOrCreate() !?

this is so weird! isn't updateOrCreate method supposed to be used to overcome duplicate entry?!

I've this simple code

        $customer = Customer::updateOrCreate(['id' => $customer->id], $customer->toArray());

that somehow throws this error!

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1587' for key 'PRIMARY' (SQL: insert into woocommerce_customers (id, first_name, last_name, username, email, role, country, date_created, date_modified) values (1587, ?, ?, ***, ****, customer, ?, 2023-03-22 09:09:45, 2023-03-22 09:09:45))

the astricts in the error are real data I just replaced them with stars..

I this this error occurs when much data inserted at the same time? Is there a way for updateOrCreate to work each one at a time? any solutions would be nice....

0 likes
4 replies
LaryAI's avatar
Level 58

The error message suggests that there is already a record with the same primary key value in the database. The updateOrCreate method is designed to update an existing record if it exists, or create a new one if it doesn't. In this case, it seems like there is already a record with the same ID as the one you're trying to update.

To fix this, you can try using the find method to retrieve the existing record and update it manually, or you can use the firstOrCreate method to create a new record if it doesn't exist, and then update it.

Here's an example using firstOrCreate:

$customer = Customer::firstOrCreate(['id' => $customer->id]);
$customer->fill($customer->toArray());
$customer->save();

This will create a new record if one with the same ID doesn't exist, or retrieve the existing one and update it with the new data.

Alternatively, you can use find to retrieve the existing record and update it manually:

$customer = Customer::find($customer->id);
if ($customer) {
    $customer->fill($customer->toArray());
    $customer->save();
} else {
    $customer = Customer::create($customer->toArray());
}

This will retrieve the existing record with the same ID, update it with the new data, and save it. If the record doesn't exist, it will create a new one.

1 like
tykus's avatar

I've this simple code

I this this error occurs when much data inserted at the same time?

What is the context for the simple code; is it executed in a loop; and where does the data come from? Why are you determining the ID value at all?

updateOrCreate is a two step operation; a read followed by a write; if there is a record with the same ID created after the read, but before the write, then this error can occur.

frankcovarrubias's avatar

Check the data model in Laravel and make sure that you have explicit which column will be used as the index or search column, otherwise it will give you this error

In your case it would be something like this:

protected $primaryKey = 'id';

in the Customer model

Please or to participate in this conversation.