-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documented examples for common denormalizations #26
Comments
I think something like this does what you're after: @pgtrigger.register(
pgtrigger.Trigger(
name='keep_num_comments_in_sync',
operation=pgtrigger.Update | pgtrigger.Insert | pgtrigger.Delete,
when=pgtrigger.After,
func='''
IF TG_OP IN ('DELETE', 'UPDATE') THEN
UPDATE blog_blogentry SET num_comments = num_comments - 1 WHERE id = OLD.entry_id;
END IF;
IF TG_OP IN ('INSERT', 'UPDATE') THEN
UPDATE blog_blogentry SET num_comments = num_comments + 1 WHERE id = NEW.entry_id;
END IF;
RETURN NULL;
''',
)
) Deals with someone moving a comment from one blog entry to another as well. Could have 3 separate triggers without the IFs if that's preferable. |
@dracos very clever for the update case! Yes, you can do three as well and then have a condition on the update to only fire when the entry has been changed. @simonw Did this address your issue? I can add it as a more advanced use case to the docs since this is also a problem I address with pgtrigger all the time (precomputing balances, etc). I'm open to ideas of adding something like this as a core trigger if it can be generalized too. |
I have a similar need with a twist: we have several "container" models that need to do things like "this object's price is the sum of all its components' prices" etc, so I can't directly use the examples I've seen around the web, as they all seem to directly use the individual value of the row being updated. My initial attempt is this (note that I will need to add Delete support as well, but first I should get it to work)
There are several omissions (eg. it's not generalized at all, and I think I might have to add a filter to ensure only the relevant container is updated) but the main problem is that the above doesn't include the current component being saved - that is, if I save component1 with UPDATE: I also tried with a statement level trigger but, unfortunately, the result is the same, so I have no idea how to proceed. |
I'm principally interested in using this library for denormalizations - thinks like:
Where that
num_comments
column stores the number of comments, and is updated by a trigger any time a comment is added or deleted.It would be great if the documentation included some examples of these! Happy to contribute some if I end up figuring this out for my project.
The text was updated successfully, but these errors were encountered: