0

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.