4 ms·
Rails migrations do not support creating foreign keys in the database, but you can do it with this library: https://github.com/matthuhiggins/foreigner https://g
by electrum 13y ago
Rails migrations do not support creating foreign keys in the database, but you can do it with this library: https://github.com/matthuhiggins/foreigner https://github.com/matthuhiggins/foreigner
- gingerlime 13y agoI was about to suggest the same thing. And if you go with foreigner, then you can / should also use the aptly-named immigrant[1] gem to automatically detect where foreign keys are missing and create the migrations for you. [1]https://github.com/jenseng/immigrant https://github.com/jenseng/immigrant
- bratsche 13y agoAnyone know why ActiveRecord doesn't just include this functionality already? Or does anyone have any idea if it's planned to add?
- tominated 13y agoI was searching for the same thing myself recently, and it seems that DHH is against having any sort of logic in the database - he believes that all validations and constraints should be in the application instead of being in both (which I believe is the correct way).
- xentronium 13y agoThe problem is that sometimes you simply can't do it properly in application. Uniqueness is the most obvious example, although not the only one.
- tominated 13y agoI probably phrased that incorrectly. I definitely think that you should have validation logic at the database level. I've had a couple of major issues in the past (while still learning web dev) where lack of indexes/constraints has pretty much ruined my database when I tried use them in production
- jeltz 13y agoYou also want to implement foreign keys in the database since you do not want to have to remember to lock referenced rows when inserting new referring rows. The database helps you out with concurrency issues.