[sqlalchemy] Using a backref-generated attribute in an event listener

2015-06-29 Thread Adrian
I tried this code:

@listens_for(AttachmentFolder.all_attachments, 'append')
def _attachment_added(target, value, *unused):
target.modified_dt = now_utc()

However AttachmentFolder.all_attachments is a backref so it doesn't exist 
at import time (I usually
register listeners right after the definition of the model class). So I 
tried using the mapper_configured
event, but backrefs still don't exist at that point.

Besides flipping the relationship/backref around (which would work only 
because I don't have
any listeners on the relationship itself yet), is there any clean way to 
bind an event listener
to a backref?

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Using a backref-generated attribute in an event listener

2015-06-29 Thread Mike Bayer



On 6/29/15 5:37 AM, Adrian wrote:

I tried this code:

@listens_for(AttachmentFolder.all_attachments, 'append')
def _attachment_added(target, value, *unused):
target.modified_dt = now_utc()

However AttachmentFolder.all_attachments is a backref so it doesn't 
exist at import time (I usually
register listeners right after the definition of the model class). So 
I tried using the mapper_configured

event, but backrefs still don't exist at that point.

Besides flipping the relationship/backref around (which would work 
only because I don't have
any listeners on the relationship itself yet), is there any clean way 
to bind an event listener

to a backref?
During mapper_configured(), backrefs which *originate* from that mapper 
should have been built up, however the receiving mapper may have already 
been invoked on mapper_configured() before this other mapper adds the 
backref relationship.


For this reason the SQLAlchemy documentation (outside of the tutorial) 
recommends to forego the use of backref() and use two relationship() 
objects instead with back_populates; this is clearer and simpler.


Another event you can use is the after_configured() event which fires 
once *all* mappers are set up, that would work also.





--
You received this message because you are subscribed to the Google 
Groups sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to sqlalchemy+unsubscr...@googlegroups.com 
mailto:sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com 
mailto:sqlalchemy@googlegroups.com.

Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.