Hacker News new | ask | show | jobs
by obsurveyor 3249 days ago
I'd like to hear more about how you would use constraints in Postgres to control state transitions in the accumulating snapshot table.
1 comments

Off the top of my head there a number ways to achieve this: The first would be to use CHECK constraints on your state fields to ensure that the appropriate states have timestamps to ensure they have been reached. Other logic could be included as well if necessary.

A second option would be to have a transaction table to record each transition (and a reference table containing all valid states), and then have foreign keys in the accumulating snapshot table to the transaction table. Although this would be kind of silly for the current problem due to its simplicity.

A third option would be to use an [is_valid] calculated field to record whether or not the state is valid.

I'm sure I could come up with more, but it's all a silly exercise because I would do (and recommend) none of these and keep business logic where business logic belongs. This would allow for more flexibility for different "kinds" of orders, more complex validation, etc.