Recent Posts by Shawn Oster

Subscribe to Recent Posts by Shawn Oster 1 post found

Jan 10, 2008
Avatar Shawn Oster 1 post

Topic: General Ruby/Rails Discussion / MySQL Error 150 when trying to create foreign keys in migration

I use constraints because in most databases I know they make for faster queries but the biggest reason is because I can’t say Rails will be the only way I interact with the database, nor that I’ll be the only person having to deal with it. Most other frameworks and tools do take foreign keys into account and so it seems like a missed oppurtunity to ignore a simple feature. It’s a common courtesy to the next developer that has to deal with what I’ve created.

The other day someone wanted to see a Visio diagram of the schema and without any FKs Visio wouldn’t have been able to model it correctly. Another example is recently I’ve had to work with a new ERP system and for the missing holes in the API we have to connect directly to the database and without those FKs things would have been a pain in the arse. I view foreign keys as a form of self-documenting code.

They almost always make your database faster, they’re cheap, they’re self-documenting, almost every other tool and framework uses them to good effect so I can’t see any good reason to not use them.