E
E
EVOSandru62015-07-24 14:04:11
Yii
EVOSandru6, 2015-07-24 14:04:11

Why in yii when create in postgres swears at the uniqueness of the key?

Good afternoon,
There is such a user registration action, I get into it:

public function actionRegistration()
    {
        $model = new Users('registration');
        if(isset($_POST['Users']))
        {
            $model->setAttributes($_POST['Users'], true); 

            if($model->save())
                Yii::app()->user->setFlash('success', 'Вы успешно зарегистрировались!');
            else
            {
                $msg = '';
                foreach($model->getErrors() as $error)
                    $msg .= $error[0];
                Yii::app()->user->setFlash('error', 'Не удалось зарегистрировать Вас <hr>'. $msg);
            }
        }
        $this->redirect(Yii::app()->request->urlReferrer);
    }

But when saving, the following error occurs:
CDbCommand failed to execute SQL query: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "pk_users_id"
DETAIL: Key (id)=(7) already exists.. The SQL statement executed was: INSERT INTO "m_users" ("status", "name", "email", "role_id", "password") VALUES (:yp0, :yp1, :yp2, :yp3, :yp4)

id has type serial NOT NULL . For some reason, the record does not go to this table, everything is fine with the rest.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LAV45, 2015-07-30
@LAV45

If during migration you added a bunch of users, then you need to reset the primary key counter

// @see \yii\db\pgsql\QueryBuilder::resetSequence()
$sql_resetSequence = Yii::$app->db->queryBuilder->resetSequence($table, $value);
Yii::$app->db->createCommand($sql_resetSequence)->execute();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question