Database triggers is a common tool to use in such cases
Ask HN: What's your preferred way of ensuring complex database integrity?
21–30 of 58 posts
Re: Ask HN: What's your preferred way of ensuring complex database integrity?
#22You use functions and stored procedures to provide a controlled API over the table. All actions will go through those and that will have your logic to enforce the constraints.
Re: Ask HN: What's your preferred way of ensuring complex database integrity?
#23I try to keep code out of my data where possible. No triggers, no constraints (excepting maybe PRIMARY KEY and NOT NULL). Despite otherwise being a nutter for type safety, I'll even favour TEXT over enums. The main reason is change. As I evolve the code base, I might want to do something today which is principled, but seemed unprincipled yesterday. Database constraints have tended to hit me the hardest at the worst t…
No triggers, no constraints (excepting maybe PRIMARY KEY and NOT NULL) Could you not then simply use a nosql solution?
Re: Ask HN: What's your preferred way of ensuring complex database integrity?
#24Some might suggest that you take a performance hit by not using stored procs etc, but in reality it's never been a problem for my apps (the largest one has over 150k active users - https://usebx.com).
Re: Ask HN: What's your preferred way of ensuring complex database integrity?
#25Re: Ask HN: What's your preferred way of ensuring complex database integrity?
#26Reference tables for things that don't change or change very seldom are a valuable tool if data across other tables must agree. Check constraints otherwise.
But that's just the bones -- the last line of defense. Having a framework that can push such constraints through the application is very helpful so you are not waiting for SQL errors to enforce integrity.
It is easy to go overboard, though, and it is worth thinking about how important data integrity and agreement actually is before writing it in stone, so to speak.
Re: Ask HN: What's your preferred way of ensuring complex database integrity?
#27As mentioned by the other comment, it's standard to use database triggers to ensure consistency. There are two major problems with dealing with them in the application side 1. You can no longer guarantee consistency. 2. In the case where you need to do some things procedurally so that your application can no longer make a yes/no query to determine if a constraint is violated but need to make use of intermediate resul…
shudder
Triggers are almost always the wrong level to handle consistency. It flies in the face of 'fail fast' if you need to actually handle problems of consistency and if you need 'last line' protection, why not bake it into the structure instead?
Re: Ask HN: What's your preferred way of ensuring complex database integrity?
#28Re: Ask HN: What's your preferred way of ensuring complex database integrity?
#29If data needs to agree PKs and FKs are a way to go. I avoid UNIQUES unless absolutely necessary since they are not structural. Reference tables for things that don't change or change very seldom are a valuable tool if data across other tables must agree. Check constraints otherwise. But that's just the bones -- the last line of defense. Having a framework that can push such constraints through the application is very…
Re: Ask HN: What's your preferred way of ensuring complex database integrity?
#30If the application is the only way to access the data, it would appear consistent until it wasn't. Assuming two application processes, process one would not immediately see the mistake it made, but process two would see it on startup. So, an audit table might be a good idea to see which process (or user) made which change, including the original erroneous entry, and any change needed to remedy the failed constraint. If the first process is still active, the second process could warn the first process, but that leaves a lot to chance. In comparison, once in place, relational database constraints are always "on" and enforced globally. Enforcement on a per transaction basis might provide a consistent slowdown, but still be preferable to a long startup time for checking the integrity of a large dataset with potentially many errors. In order to handle increasing startup times given boot time integrity checking, you may have to partition the data into more recently and less recently accessed data. In that case, you may find inconsistencies in reporting where the application only touched an earlier set of data once, and didn't enforce integrity checking. On the other hand, you could be presented with an opportunity to remedy the data before you can receive the report. If the entries that are permitted change over time, you would need some kind of date ranges on those values, so that the person receiving the report doesn't enter historically inaccurate data while fixing their own report.