We do not use foreign keys (2016)
github.com
We do not use foreign keys (2016)
1–10 of 337 posts
Re: We do not use foreign keys (2016)
#2Re: We do not use foreign keys (2016)
#3Re: We do not use foreign keys (2016)
#4The main criticism seems to be that the FK relationship makes migrating the referenced table difficult. But why not remove the FK with ALTER TABLE before the migration, migrate, and add the FK back again (which will catch any missing primary keys), preferably inside a transaction?
Re: We do not use foreign keys (2016)
#5As a counter point to the linked issue, I operate a few small applications. Foreign-keys (and constraints in general) are great at ensuring that invalid data doesn't find its way into your database. Yes, they have a performance cost. Yes, they make sharding more difficult. In my experience, at smaller scale the trade-offs are worth it. YMMV
Re: We do not use foreign keys (2016)
#6The main criticism seems to be that the FK relationship makes migrating the referenced table difficult. But why not remove the FK with ALTER TABLE before the migration, migrate, and add the FK back again (which will catch any missing primary keys), preferably inside a transaction?
MySQL does not support transactional DDL, unfortunately.
Re: We do not use foreign keys (2016)
#7Re: We do not use foreign keys (2016)
#8I'm sorry if this question sounds silly, but I've never heard this line of reasoning before and I'm fascinated by it.
Re: We do not use foreign keys (2016)
#9Re: We do not use foreign keys (2016)
#10I feel like this was a stopgap on the way to eliminating FKs. I have no idea what his roadmap was because I wasn't in that group. But his process made for good thought fodder.