On Apr 11, 2012, at 7:45 AM, Franck wrote:

> Hi folks,
> 
> I have a structure where a poll is associated with one vote per user, and 
> each vote is associated with several results.
> To manipulate this as easily as possible I use attribute_mapped_collection 
> and association_proxy - please see the attached file or here : 
> http://pastebin.com/CR2PCbCZ
> 
> It works great but I have a question. The POLL_VOTES table, i.e. my 
> association table between the polls & the results, has a special field called 
> VOTE_DT. It's correctly populated at insert time but I'd like it to be 
> automatically updated whenever the underlying collection (the results) is 
> updated.
> 
> Is it possible ? I documented the expected behaviour in the code snippet 
> (bottom of the code).

yeah there's a couple of things here -

class PollVote(Base):
    __tablename__ = "POLL_VOTES"
    vote_dt = Column(DateTime, default=datetime.datetime.now())

    def __init__(self, user=None, result_values=None):
        self.user = user
        self.result_values = result_values


First off when you use "default", you need to pass it a Python function, so 
above you'd want to say "datetime.now", without actually calling now().

The other thing is, from an ORM perspective the __init__() is the in-Python 
equivalent to "a new row".  So just set the date there (this time actually 
calling now()):

class PollVote(Base):
    __tablename__ = "POLL_VOTES"
    vote_dt = Column(DateTime, default=datetime.datetime.now)

    def __init__(self, user=None, result_values=None):
        self.user = user
        self.result_values = result_values
        self.vote_dt = datetime.datetime.now()



-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to