[sqlalchemy] sqlalchemy.exc.InvalidRequestError: Mapper properties (i.e. deferred,column_property(), relationship(), etc.) must be declared as @declared_attr callables on declarative mixin classes.
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") class Country(BaseCountry, Base): """ Managed through Kotti """ __tablename__ = 'countries' This gives me File "/data/home/ajung/src/kotti/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py", line 55, in __init__ _as_declarative(cls, classname, cls.__dict__) File "/data/home/ajung/src/kotti/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 88, in _as_declarative _MapperConfig.setup_mapping(cls, classname, dict_) File "/data/home/ajung/src/kotti/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 103, in setup_mapping cfg_cls(cls_, classname, dict_) File "/data/home/ajung/src/kotti/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 123, in __init__ self._scan_attributes() File "/data/home/ajung/src/kotti/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 216, in _scan_attributes "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. Where is the difference to the documentation? Python 2.7, Sqlalchemy 1.0.11 Andreas -- 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.
Re: [sqlalchemy] sqlalchemy.exc.InvalidRequestError: Mapper properties (i.e. deferred,column_property(), relationship(), etc.) must be declared as @declared_attr callables on declarative mixin classes
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") 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. signature.asc Description: Digital signature
Re: [sqlalchemy] sqlalchemy.exc.InvalidRequestError: Mapper properties (i.e. deferred,column_property(), relationship(), etc.) must be declared as @declared_attr callables on declarative mixin classes
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. signature.asc Description: Digital signature