|
|
|
|
|
by joevandyk
4918 days ago
|
|
Enforcing that a quote has at least one quote_line is pretty tough, at least in Postgres, and involves triggers and locks. How do you enforce that two transactions don't delete the last quote_line at the same time? And how would you enforce that the sum of the quote_lines is correct with multiple writers? Not saying it can't be done easily, but I'd love to hear how. |
|
The reason it seems simple when using a few classes is because there is an implicit assumption that concurrency is a non-issue and there's only one application involved. But those aren't good assumptions, so the approach using classes will start to look more complex (and involve locks, etc.).
In Postgres, the trick is to use SERIALIZABLE transactions everywhere (should be the default, eventually), which avoids the need for explicit locking. Then, add a trigger that is fired before changes to either table, and it would simply check that the condition holds for the quote that was modified.
Here's some code, since you asked:
Note that I did not need to add the CHECK constraint, because it's much better to use the appropriate data type -- DATERANGE -- instead of hacking it together from parts.