logo

Code for the schema you wish you had

Don't let legacy constraints leak into your new code

Joel Clermont
Joel Clermont
2026-04-07

I was recently working on a new Laravel app that also had to pull data from a legacy database with a schema that didn't conform to any Laravel conventions. Even more challenging, this legacy database was running on a different server with a completely different database technology.

That meant we couldn't enforce foreign key constraints between old and new tables or even do a simple join across the two systems. Things that would normally be simple in a single-database Laravel app just weren't possible here.

My initial approach modeling this was functional, but it was less than ideal. I skipped conventional column naming for the fields referencing foreign tables and I avoided defining relationships in Eloquent because they weren't real foreign keys anyway.

After further work in the app (and several code review sessions with Aaron), I took a step back and decided to try a different approach. I was going to model our new database as if the legacy system wasn't stuck in the past. I refactored to conventional column names, proper relationship methods, sticking with all the normal Eloquent patterns. From the outside, it looked like any other Laravel app.

The one place I acknowledged the limitation was in the migration files. I included a brief comment as to why I wasn't actually enforcing foreign key constraints on what looked like a normal foreign key column.

That single comment explained why things were different, right where a developer would look if they were wondering. But working in the app day to day, it wasn't a constant source of friction.

The broader principle is worth keeping in mind even outside of legacy integrations. When you have constraints that force you to do something unusual, try to contain the weirdness. Document it once in the right spot, and let the rest of your codebase stay conventional.

Plus, who knows, maybe in the future, that legacy system also gets rewritten in Laravel and now my conventional code is ready to integrate.

Here to help,

Joel

P.S. When you can't rely on database constraints to enforce integrity, validation picks up the slack. Our book Mastering Laravel Validation Rules can help you keep your data clean and consistent, even when your schema can't do it alone.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice.