Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a development environment disable foreign keys?

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?

like image 847
John Christensen Avatar asked May 18 '09 20:05

John Christensen


People also ask

Should you always use foreign keys?

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.

Do foreign keys hurt performance?

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.

Can we disable foreign key constraint?

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.

What problems do foreign keys introduce?

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.


3 Answers

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.

like image 59
Mitchel Sellers Avatar answered Oct 06 '22 17:10

Mitchel Sellers


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.

like image 34
Paul Sonier Avatar answered Oct 06 '22 16:10

Paul Sonier


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.

like image 28
CodeLikeBeaker Avatar answered Oct 06 '22 16:10

CodeLikeBeaker