So while we're using foreign keys in our current project, I've heard the argument before that enabling foreign key checking within a development environment simply puts roadblocks in front of developers - code should not rely on foreign keys being in place.
I was wondering what people thought about this idea - when developing, do you keep foreign keys enabled in your development environment, or do you turn them off?
Yes, you should. Foreign keys are just constrains which helps you to make relationships and be sure that you have correct information in your database. You should use them to prevent incorrect data entry from whatsoever.
It's a common mistake to avoid creating foreign keys in a database because they negatively impact the performance. It is true that foreign keys will impact INSERT, UPDATE and DELETE statements because they are data checking, but they improve the overall performance of a database.
You can disable a foreign key constraint during INSERT and UPDATE transactions in SQL Server by using SQL Server Management Studio or Transact-SQL. Use this option if you know that new data will not violate the existing constraint or if the constraint applies only to the data already in the database.
Foreign key problems. Many database users encounter foreign key errors, often due to referential integrity problems. A foreign key might point to data that no longer exists, or the foreign key's data type doesn't match the primary key data type, eroding referential integrity.
I ALWAYS keep them enabled. You want to make sure that your code isn't going to do something that violates FK rules. If you have them removed there is nothing preventing the code from putting in bad data. Additionally we will use tools such as CodeSmith to help with some of our automation pieces, and with CodeSmith, it can automatically generate query procedures for us if we have the FK's in place.
Overall, I am a firm believer that the development environment should be a very close replica to that of the production environment. If you don't have them it is very possible to have code that will fail in production because it violates a FK constraint, and it will work just fine in test.
Absolutely keep them enabled. The existence of foreign key constraints is actually very useful in development; not using the constraints can allow for lazy development; moreover, it can allow for some serious problems to creep into the code, that could cause some difficulties when moving to a production environment.
I always keep FK's enabled in my environment. The reason for this is if I code around not having FK's something could break later with them turned on.
Even though it may cause a little bit more headache to have them turned on, it is worth it in the long run in my opinion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With