I’ve created a basic database structured as follows.
vehicle_makes (id, name, slug)
vehicle_models (id, name, slug, make_id)
vehicle_trims (id, name, slug, model_id)
When attempting to create the vehicle_trims table via Laravel migrations, I get an error about duplicate foreign key constraint.
SQLSTATE[HY000]: General error: 1826 Duplicate foreign key constraint name 'id' (Connection: mysql, SQL: alter table `vehicle_trims` add constraint `id` foreign key (`model_id`) references `vehicle_models` (`id`) on delete cascade on update cascade)
After reading similar posts from other users here, I understand that the FK name has to be unique, but this error seems to be related to each table having a standard ‘id’ column.
Does that mean tables needs a unique ID such as make_id, model_id, etc. to use foreign keys, and then reference these with fk_make_id, fk_model_id and so on instead?