Reputation: 307
I have a script that saves and updates data to a Postgres database using upserts:
messageins = insert(Message).values(
id = message.id,
iserased = message.iserased,
erasedby = eraserid,
author = message.author.id,
body = message.body,
private = message.isprivate,
created = convTime(message.timecreated),
edited = convTime(message.timeedited),
lobby = message.lobbyid,
usermentions = message.mentions,
)
message_dict = {
c.name: c
for c in messageins.excluded
}
update_message = messageins.on_conflict_do_update(
index_elements = ["id"],
set_=message_dict
)
newdb.execute(update_message)
I'm trying to run a function on the before_update event:
def saveHistory(mapper, connect, target):
historicaldata = dict(target)
print("SaveHistory triggered\n")
event.listen(Message, 'before_insert', saveHistory)
event.listen(Message, 'before_update', saveHistory)
The saveHistory function isn't running. I see nothing in the documentation about Postgres-specific events. Are these event listeners supposed to work with the on_conflict_do_update() method?
Upvotes: 0
Views: 280
Reputation: 307
Turns out Postgres upserts using insert() bypasses the ORM where the event hooks are. Using event hooks with this method of inserting data is not supported.
https://groups.google.com/forum/#!topic/sqlalchemy/Q1agVMU87_Q
Upvotes: 1