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

tisuchi's avatar
Level 70

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

Suddenly getting wired behaviour in one to one insertion.

I am getting SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry error during insertion.

However, it was working fine in online before and still working fine in local matching.

Any idea why?

0 likes
17 replies
jeffdavis's avatar

You are most likely trying to insert a new record in a table with an id number that matches the id number of an already existing record.

In order to give a better response, I would need to see the code that is generating this error.

jlrdw's avatar

Or another field indexed that doesn't allow duplicates.

1 like
tisuchi's avatar
Level 70

@jeffdavis

Nope. There is no record belongs to that id.

// here $user->id is last inserted ID
$userId = User::find($user->id);

if ($userId) {
            $cityUser = new Usercity;
            $cityUser->userid = $user->id; //insert last id
            $cityUser->currentcity = $userCity;
            $cityUser->currentstate = $userState;
            $cityUser->currentcountry = $userCountry;
            $cityUser->currentlatlng = $userLatLng;
            $userId->Usercity()->save($cityUser);
        }
rsands's avatar

Check the table keys on live, it is most likely got a key blocking it

1 like
tisuchi's avatar
Level 70

More specifically, here is my error message-

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '11073' for key 'PRIMARY' (SQL: insert into `usercities` (`userid`, `currentcity`, `currentstate`, `currentcountry`, `currentlatlng`, `id`, `updated_at`, `created_at`) values (11073, Petaling Jaya, 10, Malaysia, 3.1071:101.6083, 11073, 2016-11-08 03:11:05, 2016-11-08 03:11:05))
rsands's avatar

You can't have a primary key that has duplicate values. What column is set as primary?

jeffdavis's avatar

Can you post the migration for the Cityuser table?

jeffdavis's avatar

So there error message is telling you that there is already an entry with id=11073.

tisuchi's avatar
Level 70

@jeffdavis

Here is my migration code-

 Schema::create('usercities', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('userid')->unsigned();
            $table->foreign('userid')->references('id')->on('users')->onDelete('cascade');
            $table->string('currentcity')->nullable();
            $table->string('currentstate')->nullable();
            $table->string('currentcountry')->nullable();
            $table->string('currentlatlng')->nullable();
            $table->string('previouscity1')->nullable();
            $table->string('previousstate1')->nullable();
            $table->string('previouscountry1')->nullable();
            $table->string('previouslatlng1')->nullable();
            $table->string('previouscity2')->nullable();
            $table->string('previousstate2')->nullable();
            $table->string('previouscountry2')->nullable();
            $table->string('previouslatlng2')->nullable();
            $table->timestamps();
        });
tisuchi's avatar
Level 70

@jeffdavis

Yes... I was expecting that there should be an entry there... however, nothing found.

This is the result from sql query

MySQL returned an empty result set (i.e. zero rows). (Query took 0.0011 seconds.)
SELECT * FROM `usercities` WHERE `userid` = 11073
jeffdavis's avatar
Level 5

It looks like it is trying save Usercities with id=11073 and userid=11073. You already (I am assuming) have an entry in usercities with id=11073.

2 likes
tisuchi's avatar
Level 70

@jeffdavis

Yes... you are right... There is an entry which id is 11073.

I am wondering that my id field is auto increment where my last id is 12828.

jeffdavis's avatar

What about SELECT * FROM `usercities` WHERE `id` = 11073

1 like
tisuchi's avatar
Level 70

@jeffdavis

I think didn't express you properly. Sorry for that.

However, I found my mistake.

The problem was in user.php table,

Wrong Code

return $this->hasOne('App\Usercity', 'id');

** its taking id as a foreign key.

Correct Code

return $this->hasOne('App\Usercity', 'userid');

Thanks to all of you.

Please or to participate in this conversation.