sql - Php - cannot create new database record because primary key is null (autoincrement)

Solution:

You have to use mysqli insert_id, after executing the insert command. insert_id will give you the primary key of the insert.

After

$person->insert();
$address->person_id = $person->insert_id;

Answer

Solution:

At no time do you use your Address model to insert the data into your address table.

Modify your code by adding this line :

$address = new \app\models\Address(); //ADD THIS LINE
$address->person_id = $person->person_id;
//...
           

Source