On Fri, Jan 15, 2016 at 02:26:37PM +0100, Michal Petrucha wrote:
> Hi Andreas,
> 
> On Fri, Jan 15, 2016 at 02:56:39AM -0800, Andreas Jung wrote:
> > Hi,
> > 
> > I currently have the following mix-in class construction (based on the 
> > documentation 
> > http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/mixins.html
> > )
> > 
> > Base = declarative_base() 
> > 
> > class BaseCountry(object): 
> >    """ Managed through Kotti """ 
> > 
> >    @declared_attr 
> >    def __tablename__(cls): 
> >        return cls.__name__.lower() 
> > 
> >    dpis_id = Column(Integer, primary_key=True) 
> >    title =  Column(Unicode(80)) 
> >    regions = relationship("Region", 
> >                             
> > primaryjoin="Country.dpis_id==Region.country_id") 
> 
> As the error message in the exception says, this attribute needs to be
> declared as a declared_attr, like this:
>     
>     @declared_attr
>     def regions(cls):
>         return relationship("Region", 
>                             primaryjoin="Country.dpis_id==Region.country_id") 

...and just after I sent the email I realized this wouldn't work
either, because it hardcodes a fixed string as the originating table
name. Something like the following should work, though (according to
the docs I linked):

    @declared_attr
    def regions(cls):
        return relationship("Region",
            primaryjoin=lambda: cls.dpis_id == Region.country_id
        )

...or perhaps this one, if you prefer:

    @declared_attr
    def regions(cls):
        return relationship("Region",
            primaryjoin="%s.dpis_id==Region.country_id" % cls.__name__
        )

> This is stated in the following section of the docs:
> http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/mixins.html#mixing-in-relationships
> 
> Good luck,
> 
> Michal
> 
> >    "Mapper properties (i.e. deferred," 
> > sqlalchemy.exc.InvalidRequestError: Mapper properties (i.e. 
> > deferred,column_property(), relationship(), etc.) must be declared as 
> > @declared_attr callables on declarative mixin
> > classes.
> 
> -- 
> 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 https://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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Attachment: signature.asc
Description: Digital signature

Reply via email to