Yii Allow Null Foreign Key

I have such structure of db

5252

pic1.PNG

Some addresses my not have a metro (other tables have the same problem, for example "user" may not have an address, but there is constraint in "user" table)

If I add a record in mysql it’s ok. If I do the same thing using yii I get an error

Cannot add or update a child row: a foreign key constraint fails (address, CONSTRAINT fk_Address_Area1 FOREIGN KEY (area_id) REFERENCES area (id) ON UPDATE NO ACTION)

So, what’s the problem in?

You should check two things:

  1. FKs are allowed to be null (for example, area_id INT NULL REFERENCES area(id))

  2. You’re not trying to insert other things (like ‘0’) during save().

1 thing is checked and FK is allowed to be null

About second I just use generated CRUD sources. No any changes involved to the code.