4

I am using SQLAlchemy, and face a problem, that I need to handle event, when one field in table is changed. Firstly, I've tried to use code from docs:

@listen_for(Advert.status, 'set')
def handle_change_status(target, value, oldvalue, initiator):
    if value != oldvalue:
         # do smth

But this event occure any time the field is changed, even if we do not modify db. For example:

advert=Advert()
advert.status = 3  # event apear

But I need that event appear only when I commit changes:

db.session.add(advert)
db.session.commit()  # I need event occure here

I think that I can handle each 'set' event and write them in some storage, and then add event on session.commit (after_commit) and in this event handle all set events. But this approach seems not to be very neat.

May be someone has any ideas, how to solve this problem?

5
  • Could you implement a custom column? docs.sqlalchemy.org/en/rel_0_9/core/… I did this one time it order to do another thing. But you could use in order to handle setting (process_bind_param) Commented Nov 9, 2015 at 20:45
  • Yes, I can. Am I right: I should override process_bind_param method? Does this method invoke only, when data is write in db? Commented Nov 10, 2015 at 20:28
  • Yes @Igor. I think that process_bind_param is only called when the value is written to DB. Commented Nov 11, 2015 at 9:34
  • Do you want to verify is the value was changed, don't you? Well, a custom type has no access to old values. I think that before_update could fit to you. You could issue another query inside before_update in order to verify if the value has changed if another query is not a problem. Commented Nov 11, 2015 at 10:54
  • Hi from 2019. Maybe @event.listens_for(SomeClass, 'after_insert') could help here? Version 1.3 Commented Nov 5, 2019 at 23:14

1 Answer 1

2

So, I have solve this question that way:

Firstly, I've created init where save init value: def __init__(self): self._init_value = self.value

Also I've declared method, that do the same, when orm reconstruct object from db:

@orm.reconstructor def init_on_load(self): self.__init__()

And then I created events:

@event.listens_for(Model, 'after_update')
def compare_old_and_new_values(mapper, connection, target):
    if target._init_value != target.value:
        # do smth

@event.listens_for(Model, 'after_insert')
def compare_old_and_new_values(mapper, connection, target):
    if target._init_value != target.value:
        # do smth

`

I think this approach can have some misbehaviour, when multiple users change the same record in db, but this aproach save us from making additionals SQL queries to db.

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.