Db session integrity constraint violation

I just switched from cookie session to db session and I’m getting many integrity constraint violations.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '6' for key 'PRIMARY'
The SQL being executed was: UPDATE `session` SET `id`=6 WHERE  `id`='50g77234tqjiqtcgat5fg6d0ua'

Error Info: Array
    [0] => 23000
    [1] => 1062
    [2] => Duplicate entry '6' for key 'PRIMARY'

My migration:

    $this->createTable('{{%session}}', [
        'id' => $this->primaryKey(),
        'user_id' => $this->integer(),
        'expire' => $this->integer(),
        'data' => 'LONGBLOB',
        'last_write' => $this->integer(),
    ], $tableOptions);

This issue was apparently fixed with #11401 in version 2.0.14. I’m running version 2.0.27.

Do you have it for concurrent writes (in logs) or all the time? What MySQL version do you have? Is the error still there with latest Yii 2 version?

It’s still happening with latest Yii install.

The error occurs on both server and localhost.
10.1.37-MariaDB (localhost)
10.1.44-MariaDB (server)

It happens periodically. In a half hour of browsing in an attempt to see the error, I’ve encountered it a few times and gotten logged out a few times.

In order to fix it we need a way to reproduce it.

I found the cause. I set both backend and frontend to use db session and they’re using the same session table. I suppose I need to have a separate table for backend session.

…sorry, amateur mistake.

I’m not out of the woods yet. It’s doing multiple inserts per page load, so I end up with tens of rows in a short amount of time. Is this normal behavior? Once in a while it results in the integrity constraint violation.

For example, this is from a single page load:


That looks a bit weird. A single page load should not result in multiple session records.