Pros and cons of foreign key software than in the database

This causes so many problems from a development point of view, just letting the database use the foreign key. Especially during the unit test, I cannot delete the column due to foreign key restrictions, I need to create the table in such a way that the warning about closing the foreign key does not start. In fact, I do not see too much meaning, allowing the database to force foreign key restrictions. If the application was designed correctly, there should be no manual manipulations with the database, except for individual queries. I just want to make sure that I do not dig myself into the hole without having foreign key restrictions in the database and leaving this solely the responsibility of the applications. Did I miss something?

PS my real unit tests (and not those that use mockery) will discard existing tables if the structure of the underlying domain object has been changed.

+5
source share
6 answers

In my experience, if you do not use foreign keys in the database, then in the end (assuming the database is relatively large and heavily used), you will get orphaned records. It can happen in different ways, but it always happens.

If you specified correctly, there should be no performance benefits for foreign keys.

So, the question is, does the potential cost / cost / support cost / financial cost of having orphaned entries in your database outperform development and testing?

, - . , , , .

+13

, - , .

, . , .

+6

. - , .

, . , . , .

+6

, , , , - .
, , 5 - , " ", , . / , .: -)

+3

, SO: ?. [Edit]: , , - .

+1

? ? , . , - ..

Foreign keys are designed for data integrity. Data integrity is critical to being able to use data with any reliability. Databases without data integrity are useless and can cause companies to lose money. This surpasses your egocentric idea that FKs are not needed because they make the development process more difficult for you. Data is much more important than your convenience in performing tests (which can be written to account for FK).

+1
source

All Articles