validates decorator gets called twice while using merge

Issue #2533 resolved
Former user created an issue

The @validates decorator function gets called twice while using merge to add a new object to the database. I have tested this with 0.7.8 and I couldnt find any other related bugs.

Calling the following method with input "bar" results into "barbarbar"

    @validates('data')
    def _set_data(self, key, value):
        print "Validating: %s\nType is: %s" % (value, type(value))
        data = value + value
        return data

Check the test script I have attached.

Comments (5)

  1. Mike Bayer repo owner

    Without even trying this, i'd guess that you're seeing the validator called twice because there are two different Test objects at play here - the Test object you create via constructor, and the Test object generated within merge(). Note that merge() leaves the object you pass in unchanged - it is essentially meant to copy state from an outside object to an instance that's inside the session.

    you can see/confirm this effect if you also print self in your _set_data() routine.

    I'll give it a try later to confirm, but if so this would be a "worksforme". If you truly want there to be only one Test object at play you'd need to stick with add(), as well as get() if you want to check for an existing identity.

  2. Former user Account Deleted

    Ok that makes sense.

    As the value I want to store is modified in the first object and the second object cant get it back out in the original format.

    Sorry for the unnecessary bug report.

  3. Log in to comment