R
R
Risent Veber2016-08-02 18:01:48
PostgreSQL
Risent Veber, 2016-08-02 18:01:48

How can Model.firstOrNew(attr) cause duplicate key value violates unique constraint on attr fields?

Given code

private function mobKills($count, $mob_id, $player_id)
    {
        $mobKill = PlayerMobKill::firstOrNew([
            'player_id' => $player_id,
            'mob_id' => $mob_id
        ]);

        $mobKill->incrementOrSave('count', $count);
    }

results in the following error:
Next Illuminate\Database\QueryException: SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates unique constraint "player_mob_kills_player_id_mob_id_unique"
DETAIL:  Key (player_id, mob_id)=(53768, 673) already exists. (SQL: insert into "player_mob_kills" ("player_id", "mob_id", "count") values (53768, 673, 1) returning "id") in /var/www/cw-game/releases/720/vendor/laravel/framework/src/Illuminate/Database/Connection.php:651
Stack trace:
#0 /var/www/cw-game/releases/720/vendor/laravel/framework/src/Illuminate/Database/Connection.php(611): Illuminate\Database\Connection->runQueryCallback('insert into "pl...', Array, Object(Closure))
#1 /var/www/cw-game/releases/720/vendor/laravel/framework/src/Illuminate/Database/Connection.php(324): Illuminate\Database\Connection->run('insert into "pl...', Array, Object(Closure))
#2 /var/www/cw-game/releases/720/vendor/laravel/framework/src/Illuminate/Database/Connection.php(298): Illuminate\Database\Connection->select('insert into "pl...', Array, false)
#3 /var/www/cw-game/releases/720/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/PostgresProcessor.php(20): Illuminate\Database\Connection->selectFromWriteConnection('insert into "pl...', Array)
#4 /var/www/cw-game/releases/720/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1821): Illuminate\Database\Query\Processors\PostgresProcessor->processInsertGetId(Object(Illuminate\Database\Query\Builder), 'insert into "pl...', Array, 'id')
#5 [internal function]: Illuminate\Database\Query\Builder->insertGetId(Array, 'id')
#6 /var/www/cw-game/releases/720/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(949): call_user_func_array(Array, Array)
#7 /var/www/cw-game/releases/720/bootstrap/cache/compiled.php(10494): Illuminate\Database\Eloquent\Builder->__call('insertGetId', Array)
#8 /var/www/cw-game/releases/720/bootstrap/cache/compiled.php(10483): Illuminate\Database\Eloquent\Model->insertAndSetId(Object(Illuminate\Database\Eloquent\Builder), Array)
#9 /var/www/cw-game/releases/720/bootstrap/cache/compiled.php(10434): Illuminate\Database\Eloquent\Model->performInsert(Object(Illuminate\Database\Eloquent\Builder), Array)
#10 /var/www/cw-game/releases/720/app/Models/Model.php(26): Illuminate\Database\Eloquent\Model->save()
#11 /var/www/cw-game/releases/720/app/Services/Persist.php(208): App\Models\Model->incrementOrSave('count', '1')
#12 /var/www/cw-game/releases/720/app/Services/Persist.php(130): App\Services\Persist->mobKills('1', 673, 53768)

How and why does this happen?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2016-08-02
@Sanasol

Show the table, model, etc.
What is incrementOrSave, there is no such function in the latest version of Eloquent/Laravel.

M
Mikhail Osher, 2016-08-02
@miraage

Specifically this example.
Searches for or creates an entry for the given predicate.
If the entry exists, the standard increment method of the Eloquent model is called.
If the entry does not exist, the attribute with the specified value is set and saved.
In view of the fact that I wrote this method, and everything worked, I can say one thing: the problem is in the incoming data.
PS good luck there on the clicker)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question