You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently if multiple triggers are defined on :insert, for example, the second trigger overwrites the first one as the trigger naming in the auto-generated migration is based only on model and event. Adding an auto-incrementing integer at the end of the trigger name would allow for multiple triggers.
An example use case here is needing multiple pg_advisory_xact_locks on another table based on different fields in the triggering record. For example, a Message sent from from_user_id to to_user_id would need two separate locks on User to handle messages_received_count and messages_sent_count respectively. If there is another way to handle this or if multiple triggers on :insert is a bad idea, please comment.
The text was updated successfully, but these errors were encountered:
Update: Using pg_advisory_unlock_all() and related functions (depending on your database) within a given set of SQL in a single trigger seems to work fine. This overcomes the need for multiple triggers for me but interested in others' opinions.
You should be able to define multiple triggers using trigger groups ... e.g.
trigger.after(:update)do |t|
t.alldo# every row# some sqlendt.of("foo")do# some more sqlendt.where("OLD.bar != NEW.bar AND NEW.bar != 'lol'")do# some other sqlendend
Would that resolve your use case? One gotcha might be if you're doing this in a migration instead of a model, you may need to drop and recreate all the triggers for the group whenever you want to add a new one 🤔
Currently if multiple triggers are defined on
:insert
, for example, the second trigger overwrites the first one as the trigger naming in the auto-generated migration is based only on model and event. Adding an auto-incrementing integer at the end of the trigger name would allow for multiple triggers.An example use case here is needing multiple
pg_advisory_xact_lock
s on another table based on different fields in the triggering record. For example, aMessage
sent fromfrom_user_id
toto_user_id
would need two separate locks onUser
to handlemessages_received_count
andmessages_sent_count
respectively. If there is another way to handle this or if multiple triggers on:insert
is a bad idea, please comment.The text was updated successfully, but these errors were encountered: