Using mySql, I've created 3 tables: vehicles
, cars
and bikes
.
Tables cars
and bikes
are a specialization of the table vehicles
(child tables).
vehicles
has one primary key called id
.
cars
and bikes
don't have primary key. Instead they also have a field called id
with an index on it which contains the same values of vehicles.id
to act as an foreign key.
Using Laravel 5.3 framework, I'm trying to, first insert a new record into vehicles
then insert a new record into cars
like so:
$id = Vehicle::create(['name'=>'volvo'])->id;
Car::create(['id'=>$id, 'nbDoors'=>4]);
I get the following error:
QueryException in Connection.php line 770: SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into
cars
(nbDorrs
) values (3))
Is there a solution or a better database design so I can insert data in both tables and keep this specialization?
Found the solution: I simply omitted to add id to the fillable columns in the Car model!
class Car extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['id', 'nbDoors'];