Integrity constraint violation – yii\db\IntegrityException

Hello, I’m only creating a signup form that gets the Region, City/Municipal, Province and Barangay. After filling up the information on the signup form I’m getting an error “Integrity constraint violation – yii\db\IntegrityException”

Here is how I designed the db schema

    public function rules()


        return [

            ['username', 'trim'],

            ['username', 'required'],

            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],

            ['username', 'string', 'min' => 2, 'max' => 255],

            ['email', 'trim'],

            ['email', 'required'],

            ['email', 'email'],

            ['email', 'string', 'max' => 255],

            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

            ['password', 'required'],

            ['password', 'string', 'min' => 6],

            ['first_name', 'required'],

            ['first_name', 'string', 'max' => 45],

            ['middle_name', 'string', 'max' => 45],

            ['last_name', 'required'],

            ['last_name', 'string', 'max' => 45],

            ['contact', 'required'],

            ['contact', 'string', 'max' => 45],

            ['birth_date', 'required'],

            ['type', 'required'],

            ['type', 'string', 'max' => 45],

            ['external_type', 'required'],

            ['external_type', 'string', 'max' => 45],

            ['status', 'string', 'max' => 45],

            ['region_id', 'required'],

            ['barangay_id', 'required'],

            ['province_id', 'required'],

            ['city_municipal_id', 'required'],

After customizing the form and the model I’m getting the error, where did I get wrong?


The error says that you forgot to set "region_id" when you were trying to insert a "user" record.

In fact, you executed a SQL for insertion without setting many required fields like region_id, barangay_id, province_id, etc.

I wonder where this SQL comes from, since Yii’s ActiveRecord won’t execute this kind of SQL in its save() method.

Hello there, it came from the yii migration when I installed the advanced app.

I’ve also figured it out now, I’ve forgot to put $user->region = $this->region; in the signup model.