[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] hybrid property / expression returns entire table
What happens if you put the print statement immediately after the class definition? Is there any chance that you've got "nsa_logmstar = None" somewhere in your class definition? Here's a test script which appears to work: import math import sqlalchemy as sa import sqlalchemy.orm as saorm from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class Sample(Base): __tablename__ = 'sample' pk = sa.Column(sa.Integer, primary_key=True) nsa_mstar = sa.Column(sa.Float) @hybrid_property def nsa_logmstar(self): try: return math.log10(self.nsa_mstar) except ValueError: return -.0 except TypeError: return None @nsa_logmstar.expression def nsa_logmstar(cls): return sa.func.log(cls.nsa_mstar) if __name__ == '__main__': sm = saorm.sessionmaker() session = sm() print session.query(Sample.pk).filter(Sample.nsa_logmstar < 9) And here's the output: SELECT sample.pk AS sample_pk FROM sample WHERE log(sample.nsa_mstar) < :log_1 Simon On Fri, Jan 15, 2016 at 2:23 PM, Brian Cherinkawrote: > Ahh. Thanks. Here is the class side then. Still None. > > In [14]: print datadb.Sample.nsa_logmstar > None > > Brian > > On Friday, January 15, 2016 at 8:48:30 AM UTC-5, Simon King wrote: >> >> "Sample()" is an instance. "Sample" is the class. Try: >> >> print datadb.Sample.nsa_logmstar >> >> Simon >> >> On Fri, Jan 15, 2016 at 1:46 PM, Brian Cherinka >> wrote: >> >>> Hi Simon, >>> >>> Printing on the class side, I get >>> >>> In [11]: print datadb.Sample().nsa_logmstar >>> None >>> >>> It looks like it's getting set to None (or remaining None). I'm not >>> quite sure what this tells me, except that it's not working. Printing on >>> in the instance side, I get >>> >>> In [12]: print cube.sample[0].nsa_mstar >>> 138616.0 >>> >>> In [13]: print cube.sample[0].nsa_logmstar >>> 9.14181336239 >>> >>> nsa_mstar is a column in my database table, and nsa_logmstar I want to >>> be simply the log-base10 of that quantity. >>> >>> If this doesn't give any insight, then it will take me some time to >>> provide a small script. This code is embedded into a bunch of stuff. But >>> I'll work on it. >>> >>> Brian >>> >>> >>> On Friday, January 15, 2016 at 5:00:51 AM UTC-5, Simon King wrote: >>> On Fri, Jan 15, 2016 at 6:16 AM, Brian Cherinka wrote: > I'm trying to set up a hybrid property / expression in a custom class, > that I can use in queries. I think I have the syntax correct, however the > query returns the entire table, instead of the correct subset of results. > And the where clause just indicates True rather than the correct > expression. > > > Here is my hybrid property/expression definition > > class Sample(Base,ArrayOps): >__tablename__ = 'sample' >__table_args__ = {'autoload' : True, 'schema' : 'datadb'} > >def __repr__(self): >return ' >@hybrid_property >def nsa_logmstar(self): >try: return math.log10(self.nsa_mstar) >except ValueError as e: >return -.0 >except TypeError as e: >return None > >@nsa_logmstar.expression >def nsa_logmstar(cls): >return func.log(cls.nsa_mstar) > > The session query is > > session.query(Sample.pk).filter(Sample.nsa_logmstar < 9) > > But printing it does not show the appropriate condition. I get > > SELECT datadb.sample.pk AS datadb_sample_pk, > FROM datadb.sample > WHERE true > > and the results return the entire table of ~11000 rows instead of the > expected 272 rows. What's going on here? Everything looks correct to me, > but I can't figure it out. > > I'm expecting the SQL statement to look like this > > select s.pk > from datadb.sample as s > where log(s.nsa_mstar) < 9; > > Any thoughts? Thanks. > > I can't see anything obviously wrong with your code, but it looks like Sample.nsa_logmstar is not actually resolving to the hybrid property in your query. What happens if you "print Sample.nsa_logmstar" just before the query? Otherwise, please provide a small runnable script that demonstrates the problem. Simon >>> -- >>> 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+...@googlegroups.com. >>> To post to this group, send email to sqlal...@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
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
Re: [sqlalchemy] hybrid property / expression returns entire table
Ahh. Thanks. Here is the class side then. Still None. In [14]: print datadb.Sample.nsa_logmstar None Brian On Friday, January 15, 2016 at 8:48:30 AM UTC-5, Simon King wrote: > > "Sample()" is an instance. "Sample" is the class. Try: > > print datadb.Sample.nsa_logmstar > > Simon > > On Fri, Jan 15, 2016 at 1:46 PM, Brian Cherinka> wrote: > >> Hi Simon, >> >> Printing on the class side, I get >> >> In [11]: print datadb.Sample().nsa_logmstar >> None >> >> It looks like it's getting set to None (or remaining None). I'm not >> quite sure what this tells me, except that it's not working. Printing on >> in the instance side, I get >> >> In [12]: print cube.sample[0].nsa_mstar >> 138616.0 >> >> In [13]: print cube.sample[0].nsa_logmstar >> 9.14181336239 >> >> nsa_mstar is a column in my database table, and nsa_logmstar I want to be >> simply the log-base10 of that quantity. >> >> If this doesn't give any insight, then it will take me some time to >> provide a small script. This code is embedded into a bunch of stuff. But >> I'll work on it. >> >> Brian >> >> >> On Friday, January 15, 2016 at 5:00:51 AM UTC-5, Simon King wrote: >> >>> On Fri, Jan 15, 2016 at 6:16 AM, Brian Cherinka >>> wrote: >>> I'm trying to set up a hybrid property / expression in a custom class, that I can use in queries. I think I have the syntax correct, however the query returns the entire table, instead of the correct subset of results. And the where clause just indicates True rather than the correct expression. Here is my hybrid property/expression definition class Sample(Base,ArrayOps): __tablename__ = 'sample' __table_args__ = {'autoload' : True, 'schema' : 'datadb'} def __repr__(self): return '>>> @hybrid_property def nsa_logmstar(self): try: return math.log10(self.nsa_mstar) except ValueError as e: return -.0 except TypeError as e: return None @nsa_logmstar.expression def nsa_logmstar(cls): return func.log(cls.nsa_mstar) The session query is session.query(Sample.pk).filter(Sample.nsa_logmstar < 9) But printing it does not show the appropriate condition. I get SELECT datadb.sample.pk AS datadb_sample_pk, FROM datadb.sample WHERE true and the results return the entire table of ~11000 rows instead of the expected 272 rows. What's going on here? Everything looks correct to me, but I can't figure it out. I'm expecting the SQL statement to look like this select s.pk from datadb.sample as s where log(s.nsa_mstar) < 9; Any thoughts? Thanks. >>> I can't see anything obviously wrong with your code, but it looks like >>> Sample.nsa_logmstar is not actually resolving to the hybrid property in >>> your query. What happens if you "print Sample.nsa_logmstar" just before the >>> query? >>> >>> Otherwise, please provide a small runnable script that demonstrates the >>> problem. >>> >>> Simon >>> >> -- >> 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+...@googlegroups.com . >> To post to this group, send email to sqlal...@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.
Re: [sqlalchemy] hybrid property / expression returns entire table
Actually, the class definition is entirely what I posted in the original message. I didn't cut anything out of that. I don't define the columns in mine, as you did. The property nsa_logmstar is not defined anywhere else in the class or in any other place in this code, or in any code that interacts with this code. class Sample(Base,ArrayOps): __tablename__ = 'sample' __table_args__ = {'autoload' : True, 'schema' : 'datadb'} def __repr__(self): return ' > What happens if you put the print statement immediately after the class > definition? Is there any chance that you've got "nsa_logmstar = None" > somewhere in your class definition? > > Here's a test script which appears to work: > > import math > > import sqlalchemy as sa > import sqlalchemy.orm as saorm > from sqlalchemy.ext.hybrid import hybrid_property > from sqlalchemy.ext.declarative import declarative_base > > Base = declarative_base() > > class Sample(Base): > __tablename__ = 'sample' > > pk = sa.Column(sa.Integer, primary_key=True) > nsa_mstar = sa.Column(sa.Float) > > @hybrid_property > def nsa_logmstar(self): > try: > return math.log10(self.nsa_mstar) > except ValueError: > return -.0 > except TypeError: > return None > > @nsa_logmstar.expression > def nsa_logmstar(cls): > return sa.func.log(cls.nsa_mstar) > > > if __name__ == '__main__': > sm = saorm.sessionmaker() > session = sm() > print session.query(Sample.pk).filter(Sample.nsa_logmstar < 9) > > > And here's the output: > > > SELECT sample.pk AS sample_pk > FROM sample > WHERE log(sample.nsa_mstar) < :log_1 > > > Simon > > > On Fri, Jan 15, 2016 at 2:23 PM, Brian Cherinka> wrote: > >> Ahh. Thanks. Here is the class side then. Still None. >> >> In [14]: print datadb.Sample.nsa_logmstar >> None >> >> Brian >> >> On Friday, January 15, 2016 at 8:48:30 AM UTC-5, Simon King wrote: >>> >>> "Sample()" is an instance. "Sample" is the class. Try: >>> >>> print datadb.Sample.nsa_logmstar >>> >>> Simon >>> >>> On Fri, Jan 15, 2016 at 1:46 PM, Brian Cherinka >>> wrote: >>> Hi Simon, Printing on the class side, I get In [11]: print datadb.Sample().nsa_logmstar None It looks like it's getting set to None (or remaining None). I'm not quite sure what this tells me, except that it's not working. Printing on in the instance side, I get In [12]: print cube.sample[0].nsa_mstar 138616.0 In [13]: print cube.sample[0].nsa_logmstar 9.14181336239 nsa_mstar is a column in my database table, and nsa_logmstar I want to be simply the log-base10 of that quantity. If this doesn't give any insight, then it will take me some time to provide a small script. This code is embedded into a bunch of stuff. But I'll work on it. Brian On Friday, January 15, 2016 at 5:00:51 AM UTC-5, Simon King wrote: > On Fri, Jan 15, 2016 at 6:16 AM, Brian Cherinka > wrote: > >> I'm trying to set up a hybrid property / expression in a custom >> class, that I can use in queries. I think I have the syntax correct, >> however the query returns the entire table, instead of the correct >> subset >> of results. And the where clause just indicates True rather than the >> correct expression. >> >> >> Here is my hybrid property/expression definition >> >> class Sample(Base,ArrayOps): >>__tablename__ = 'sample' >>__table_args__ = {'autoload' : True, 'schema' : 'datadb'} >> >>def __repr__(self): >>return '> >>@hybrid_property >>def nsa_logmstar(self): >>try: return math.log10(self.nsa_mstar) >>except ValueError as e: >>return -.0 >>except TypeError as e: >>return None >> >>@nsa_logmstar.expression >>def nsa_logmstar(cls): >>return func.log(cls.nsa_mstar) >> >> The session query is >> >> session.query(Sample.pk).filter(Sample.nsa_logmstar < 9) >> >> But printing it does not show the appropriate condition. I get >> >> SELECT datadb.sample.pk AS datadb_sample_pk, >> FROM datadb.sample >> WHERE true >> >> and the results return the entire table of ~11000 rows instead of the >> expected 272 rows. What's going on here? Everything looks correct to >> me, >> but I can't figure it out. >> >> I'm expecting the SQL statement to look like this >> >> select s.pk >> from datadb.sample as s >> where log(s.nsa_mstar) < 9; >> >> Any thoughts? Thanks. >> >> > I can't see anything obviously wrong with your code, but it looks like > Sample.nsa_logmstar is not
Re: [sqlalchemy] hybrid property / expression returns entire table
Does my test script produce the right output for you in your installation? What does the print statement immediately after the class definition produce? Simon > On 15 Jan 2016, at 19:10, Brian Cherinkawrote: > > Actually, the class definition is entirely what I posted in the original > message. I didn't cut anything out of that. I don't define the columns in > mine, as you did. The property nsa_logmstar is not defined anywhere else in > the class or in any other place in this code, or in any code that interacts > with this code. > > class Sample(Base,ArrayOps): >__tablename__ = 'sample' >__table_args__ = {'autoload' : True, 'schema' : 'datadb'} > >def __repr__(self): >return ' >@hybrid_property >def nsa_logmstar(self): >try: return math.log10(self.nsa_mstar) >except ValueError as e: >return -.0 >except TypeError as e: >return None > >@nsa_logmstar.expression >def nsa_logmstar(cls): >return func.log(cls.nsa_mstar) > > My database connection is a singleton and my base is defined inside that, > essentially > > engine = create_engine(database_connection_string) > Base = declarative_base(bind=engine) > > Brian > > On Friday, January 15, 2016 at 9:43:39 AM UTC-5, Simon King wrote: > What happens if you put the print statement immediately after the class > definition? Is there any chance that you've got "nsa_logmstar = None" > somewhere in your class definition? > > Here's a test script which appears to work: > > import math > > import sqlalchemy as sa > import sqlalchemy.orm as saorm > from sqlalchemy.ext.hybrid import hybrid_property > from sqlalchemy.ext.declarative import declarative_base > > Base = declarative_base() > > class Sample(Base): > __tablename__ = 'sample' > > pk = sa.Column(sa.Integer, primary_key=True) > nsa_mstar = sa.Column(sa.Float) > > @hybrid_property > def nsa_logmstar(self): > try: > return math.log10(self.nsa_mstar) > except ValueError: > return -.0 > except TypeError: > return None > > @nsa_logmstar.expression > def nsa_logmstar(cls): > return sa.func.log(cls.nsa_mstar) > > > if __name__ == '__main__': > sm = saorm.sessionmaker() > session = sm() > print session.query(Sample.pk).filter(Sample.nsa_logmstar < 9) > > > And here's the output: > > > SELECT sample.pk AS sample_pk > FROM sample > WHERE log(sample.nsa_mstar) < :log_1 > > > Simon > > > On Fri, Jan 15, 2016 at 2:23 PM, Brian Cherinka wrote: > Ahh. Thanks. Here is the class side then. Still None. > > In [14]: print datadb.Sample.nsa_logmstar > None > > Brian > > On Friday, January 15, 2016 at 8:48:30 AM UTC-5, Simon King wrote: > "Sample()" is an instance. "Sample" is the class. Try: > > print datadb.Sample.nsa_logmstar > > Simon > > On Fri, Jan 15, 2016 at 1:46 PM, Brian Cherinka wrote: > Hi Simon, > > Printing on the class side, I get > > In [11]: print datadb.Sample().nsa_logmstar > None > > It looks like it's getting set to None (or remaining None). I'm not quite > sure what this tells me, except that it's not working. Printing on in the > instance side, I get > > In [12]: print cube.sample[0].nsa_mstar > 138616.0 > > In [13]: print cube.sample[0].nsa_logmstar > 9.14181336239 > > nsa_mstar is a column in my database table, and nsa_logmstar I want to be > simply the log-base10 of that quantity. > > If this doesn't give any insight, then it will take me some time to provide a > small script. This code is embedded into a bunch of stuff. But I'll work on > it. > > Brian > > > On Friday, January 15, 2016 at 5:00:51 AM UTC-5, Simon King wrote: > On Fri, Jan 15, 2016 at 6:16 AM, Brian Cherinka wrote: > I'm trying to set up a hybrid property / expression in a custom class, that I > can use in queries. I think I have the syntax correct, however the query > returns the entire table, instead of the correct subset of results. And the > where clause just indicates True rather than the correct expression. > > > > Here is my hybrid property/expression definition > > > class Sample(Base,ArrayOps): >__tablename__ = 'sample' >__table_args__ = {'autoload' : True, 'schema' : 'datadb'} > >def __repr__(self): >return ' >@hybrid_property >def nsa_logmstar(self): >try: return math.log10(self.nsa_mstar) >except ValueError as e: >return -.0 >except TypeError as e: >return None > >@nsa_logmstar.expression >def nsa_logmstar(cls): >return func.log(cls.nsa_mstar) > > The session query is > > session.query(Sample.pk).filter(Sample.nsa_logmstar < 9) > > But printing it does not show the appropriate condition. I get > > SELECT datadb.sample.pk AS datadb_sample_pk, > FROM
Re: [sqlalchemy] hybrid property / expression returns entire table
Here is the print immediately after my original class definition: print 'sample nsa log mstar', Sample.nsa_logmstar and the result sample nsa log mstar None When I run your script exactly as is, I get the same output as you. When I replace my class definition with yours, inside my code, as follows class Sample(Base): __tablename__ = 'sample' __table_args__ = {'autoload' : True, 'schema' : 'mangadatadb'} (I needed to add this line in) pk = Column(Integer, primary_key=True) nsa_mstar = Column(Float) @hybrid_property def nsa_logmstar(self): try: return math.log10(self.nsa_mstar) except ValueError: return -.0 except TypeError: return None @nsa_logmstar.expression def nsa_logmstar(cls): return func.log(cls.nsa_mstar) now the print statement : print 'sample nsa log mstar', Sample.nsa_logmstar returns sample nsa log mstar log(mangadatadb.sample.nsa_mstar) On Friday, January 15, 2016 at 4:28:31 PM UTC-5, Simon King wrote: > > Does my test script produce the right output for you in your installation? > > What does the print statement immediately after the class definition > produce? > > Simon > > > On 15 Jan 2016, at 19:10, Brian Cherinka> wrote: > > > > Actually, the class definition is entirely what I posted in the original > message. I didn't cut anything out of that. I don't define the columns in > mine, as you did. The property nsa_logmstar is not defined anywhere else > in the class or in any other place in this code, or in any code that > interacts with this code. > > > > class Sample(Base,ArrayOps): > >__tablename__ = 'sample' > >__table_args__ = {'autoload' : True, 'schema' : 'datadb'} > > > >def __repr__(self): > >return ' > > >@hybrid_property > >def nsa_logmstar(self): > >try: return math.log10(self.nsa_mstar) > >except ValueError as e: > >return -.0 > >except TypeError as e: > >return None > > > >@nsa_logmstar.expression > >def nsa_logmstar(cls): > >return func.log(cls.nsa_mstar) > > > > My database connection is a singleton and my base is defined inside > that, essentially > > > > engine = create_engine(database_connection_string) > > Base = declarative_base(bind=engine) > > > > Brian > > > > On Friday, January 15, 2016 at 9:43:39 AM UTC-5, Simon King wrote: > > What happens if you put the print statement immediately after the class > definition? Is there any chance that you've got "nsa_logmstar = None" > somewhere in your class definition? > > > > Here's a test script which appears to work: > > > > import math > > > > import sqlalchemy as sa > > import sqlalchemy.orm as saorm > > from sqlalchemy.ext.hybrid import hybrid_property > > from sqlalchemy.ext.declarative import declarative_base > > > > Base = declarative_base() > > > > class Sample(Base): > > __tablename__ = 'sample' > > > > pk = sa.Column(sa.Integer, primary_key=True) > > nsa_mstar = sa.Column(sa.Float) > > > > @hybrid_property > > def nsa_logmstar(self): > > try: > > return math.log10(self.nsa_mstar) > > except ValueError: > > return -.0 > > except TypeError: > > return None > > > > @nsa_logmstar.expression > > def nsa_logmstar(cls): > > return sa.func.log(cls.nsa_mstar) > > > > > > if __name__ == '__main__': > > sm = saorm.sessionmaker() > > session = sm() > > print session.query(Sample.pk).filter(Sample.nsa_logmstar < 9) > > > > > > And here's the output: > > > > > > SELECT sample.pk AS sample_pk > > FROM sample > > WHERE log(sample.nsa_mstar) < :log_1 > > > > > > Simon > > > > > > On Fri, Jan 15, 2016 at 2:23 PM, Brian Cherinka > wrote: > > Ahh. Thanks. Here is the class side then. Still None. > > > > In [14]: print datadb.Sample.nsa_logmstar > > None > > > > Brian > > > > On Friday, January 15, 2016 at 8:48:30 AM UTC-5, Simon King wrote: > > "Sample()" is an instance. "Sample" is the class. Try: > > > > print datadb.Sample.nsa_logmstar > > > > Simon > > > > On Fri, Jan 15, 2016 at 1:46 PM, Brian Cherinka > wrote: > > Hi Simon, > > > > Printing on the class side, I get > > > > In [11]: print datadb.Sample().nsa_logmstar > > None > > > > It looks like it's getting set to None (or remaining None). I'm not > quite sure what this tells me, except that it's not working. Printing on > in the instance side, I get > > > > In [12]: print cube.sample[0].nsa_mstar > > 138616.0 > > > > In [13]: print cube.sample[0].nsa_logmstar > > 9.14181336239 > > > > nsa_mstar is a column in my database table, and nsa_logmstar I want to > be simply the log-base10 of that quantity. > > > > If this doesn't give any
Re: [sqlalchemy] hybrid property / expression returns entire table
It looks like I needed to define the columns inside my class. That's the only difference between your class and mine. And I tested out the query and it now works, and returns the correct number of rows. In [4]: print session.query(datadb.Sample.pk).filter(datadb.Sample.nsa_logmstar < 9) SELECT datadb.sample.pk AS datadb_sample_pk FROM datadb.sample WHERE log(datadb.sample.nsa_mstar) < %(log_1)s In [6]: len(session.query(datadb.Sample.pk).filter(datadb.Sample.nsa_logmstar < 9,datadb.Sample.nsa_mstar > 0).all()) Out[6]: 273 Do you have any idea why the column definition matters here? Thanks for all your help. Brian On Friday, January 15, 2016 at 5:02:03 PM UTC-5, Brian Cherinka wrote: > > Here is the print immediately after my original class definition: > > print 'sample nsa log mstar', Sample.nsa_logmstar > > and the result > > sample nsa log mstar None > > When I run your script exactly as is, I get the same output as you. > > When I replace my class definition with yours, inside my code, as follows > > class Sample(Base): > __tablename__ = 'sample' > __table_args__ = {'autoload' : True, 'schema' : 'mangadatadb'} (I > needed to add this line in) > > pk = Column(Integer, primary_key=True) > nsa_mstar = Column(Float) > > @hybrid_property > def nsa_logmstar(self): > try: > return math.log10(self.nsa_mstar) > except ValueError: > return -.0 > except TypeError: > return None > > @nsa_logmstar.expression > def nsa_logmstar(cls): > return func.log(cls.nsa_mstar) > > now the print statement : print 'sample nsa log mstar', > Sample.nsa_logmstar > returns > > sample nsa log mstar log(mangadatadb.sample.nsa_mstar) > > > On Friday, January 15, 2016 at 4:28:31 PM UTC-5, Simon King wrote: >> >> Does my test script produce the right output for you in your >> installation? >> >> What does the print statement immediately after the class definition >> produce? >> >> Simon >> >> > On 15 Jan 2016, at 19:10, Brian Cherinkawrote: >> > >> > Actually, the class definition is entirely what I posted in the >> original message. I didn't cut anything out of that. I don't define the >> columns in mine, as you did. The property nsa_logmstar is not defined >> anywhere else in the class or in any other place in this code, or in any >> code that interacts with this code. >> > >> > class Sample(Base,ArrayOps): >> >__tablename__ = 'sample' >> >__table_args__ = {'autoload' : True, 'schema' : 'datadb'} >> > >> >def __repr__(self): >> >return '> > >> >@hybrid_property >> >def nsa_logmstar(self): >> >try: return math.log10(self.nsa_mstar) >> >except ValueError as e: >> >return -.0 >> >except TypeError as e: >> >return None >> > >> >@nsa_logmstar.expression >> >def nsa_logmstar(cls): >> >return func.log(cls.nsa_mstar) >> > >> > My database connection is a singleton and my base is defined inside >> that, essentially >> > >> > engine = create_engine(database_connection_string) >> > Base = declarative_base(bind=engine) >> > >> > Brian >> > >> > On Friday, January 15, 2016 at 9:43:39 AM UTC-5, Simon King wrote: >> > What happens if you put the print statement immediately after the class >> definition? Is there any chance that you've got "nsa_logmstar = None" >> somewhere in your class definition? >> > >> > Here's a test script which appears to work: >> > >> > import math >> > >> > import sqlalchemy as sa >> > import sqlalchemy.orm as saorm >> > from sqlalchemy.ext.hybrid import hybrid_property >> > from sqlalchemy.ext.declarative import declarative_base >> > >> > Base = declarative_base() >> > >> > class Sample(Base): >> > __tablename__ = 'sample' >> > >> > pk = sa.Column(sa.Integer, primary_key=True) >> > nsa_mstar = sa.Column(sa.Float) >> > >> > @hybrid_property >> > def nsa_logmstar(self): >> > try: >> > return math.log10(self.nsa_mstar) >> > except ValueError: >> > return -.0 >> > except TypeError: >> > return None >> > >> > @nsa_logmstar.expression >> > def nsa_logmstar(cls): >> > return sa.func.log(cls.nsa_mstar) >> > >> > >> > if __name__ == '__main__': >> > sm = saorm.sessionmaker() >> > session = sm() >> > print session.query(Sample.pk).filter(Sample.nsa_logmstar < 9) >> > >> > >> > And here's the output: >> > >> > >> > SELECT sample.pk AS sample_pk >> > FROM sample >> > WHERE log(sample.nsa_mstar) < :log_1 >> > >> > >> > Simon >> > >> > >> > On Fri, Jan 15, 2016 at 2:23 PM, Brian Cherinka >> wrote: >> > Ahh. Thanks. Here is the class side then. Still None. >> > >> > In [14]: print datadb.Sample.nsa_logmstar >> > None >> > >> > Brian >> > >> > On Friday,
Re: [sqlalchemy] hybrid property / expression returns entire table
You shouldn’t need to define the columns. Here’s another test script: ### import math import sqlalchemy as sa import sqlalchemy.orm as saorm from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() engine = sa.create_engine('sqlite:///hybridtest.db') engine.execute(""" CREATE TABLE sample ( pk INTEGER NOT NULL, nsa_mstar FLOAT, PRIMARY KEY (pk) ) """) class Sample(Base): __tablename__ = 'sample' __table_args__ = {'autoload' : True, 'autoload_with': engine} @hybrid_property def nsa_logmstar(self): try: return math.log10(self.nsa_mstar) except ValueError: return -.0 except TypeError: return None @nsa_logmstar.expression def nsa_logmstar(cls): return sa.func.log(cls.nsa_mstar) if __name__ == '__main__': sm = saorm.sessionmaker() session = sm() print session.query(Sample.pk).filter(Sample.nsa_logmstar < 9) ### What database are you using, and what is your SQL table definition? Does your table already have a nsa_logmstar column? (I don’t think that should matter, but it would be worth checking) Simon > On 15 Jan 2016, at 22:27, Brian Cherinkawrote: > > It looks like I needed to define the columns inside my class. That's the > only difference between your class and mine. And I tested out the query and > it now works, and returns the correct number of rows. > > In [4]: print > session.query(datadb.Sample.pk).filter(datadb.Sample.nsa_logmstar < 9) > > SELECT datadb.sample.pk AS datadb_sample_pk > FROM datadb.sample > WHERE log(datadb.sample.nsa_mstar) < %(log_1)s > > In [6]: len(session.query(datadb.Sample.pk).filter(datadb.Sample.nsa_logmstar > < 9,datadb.Sample.nsa_mstar > 0).all()) > Out[6]: 273 > > Do you have any idea why the column definition matters here? Thanks for all > your help. > > Brian > > On Friday, January 15, 2016 at 5:02:03 PM UTC-5, Brian Cherinka wrote: > Here is the print immediately after my original class definition: > > print 'sample nsa log mstar', Sample.nsa_logmstar > > and the result > > sample nsa log mstar None > > When I run your script exactly as is, I get the same output as you. > > When I replace my class definition with yours, inside my code, as follows > > class Sample(Base): > __tablename__ = 'sample' > __table_args__ = {'autoload' : True, 'schema' : 'mangadatadb'} (I needed > to add this line in) > > pk = Column(Integer, primary_key=True) > nsa_mstar = Column(Float) > > @hybrid_property > def nsa_logmstar(self): > try: > return math.log10(self.nsa_mstar) > except ValueError: > return -.0 > except TypeError: > return None > > @nsa_logmstar.expression > def nsa_logmstar(cls): > return func.log(cls.nsa_mstar) > > now the print statement : print 'sample nsa log mstar', Sample.nsa_logmstar > returns > > sample nsa log mstar log(mangadatadb.sample.nsa_mstar) > > > On Friday, January 15, 2016 at 4:28:31 PM UTC-5, Simon King wrote: > Does my test script produce the right output for you in your installation? > > What does the print statement immediately after the class definition produce? > > Simon > > > On 15 Jan 2016, at 19:10, Brian Cherinka wrote: > > > > Actually, the class definition is entirely what I posted in the original > > message. I didn't cut anything out of that. I don't define the columns in > > mine, as you did. The property nsa_logmstar is not defined anywhere else > > in the class or in any other place in this code, or in any code that > > interacts with this code. > > > > class Sample(Base,ArrayOps): > >__tablename__ = 'sample' > >__table_args__ = {'autoload' : True, 'schema' : 'datadb'} > > > >def __repr__(self): > >return ' > > >@hybrid_property > >def nsa_logmstar(self): > >try: return math.log10(self.nsa_mstar) > >except ValueError as e: > >return -.0 > >except TypeError as e: > >return None > > > >@nsa_logmstar.expression > >def nsa_logmstar(cls): > >return func.log(cls.nsa_mstar) > > > > My database connection is a singleton and my base is defined inside that, > > essentially > > > > engine = create_engine(database_connection_string) > > Base = declarative_base(bind=engine) > > > > Brian > > > > On Friday, January 15, 2016 at 9:43:39 AM UTC-5, Simon King wrote: > > What happens if you put the print statement immediately after the class > > definition? Is there any chance that you've got "nsa_logmstar = None" > > somewhere in your class definition? > > > > Here's a test script which appears to work: > > > > import math > > > > import
Re: [sqlalchemy] hybrid property / expression returns entire table
On Fri, Jan 15, 2016 at 6:16 AM, Brian Cherinkawrote: > I'm trying to set up a hybrid property / expression in a custom class, > that I can use in queries. I think I have the syntax correct, however the > query returns the entire table, instead of the correct subset of results. > And the where clause just indicates True rather than the correct > expression. > > > Here is my hybrid property/expression definition > > class Sample(Base,ArrayOps): >__tablename__ = 'sample' >__table_args__ = {'autoload' : True, 'schema' : 'datadb'} > >def __repr__(self): >return ' >@hybrid_property >def nsa_logmstar(self): >try: return math.log10(self.nsa_mstar) >except ValueError as e: >return -.0 >except TypeError as e: >return None > >@nsa_logmstar.expression >def nsa_logmstar(cls): >return func.log(cls.nsa_mstar) > > The session query is > > session.query(Sample.pk).filter(Sample.nsa_logmstar < 9) > > But printing it does not show the appropriate condition. I get > > SELECT datadb.sample.pk AS datadb_sample_pk, > FROM datadb.sample > WHERE true > > and the results return the entire table of ~11000 rows instead of the > expected 272 rows. What's going on here? Everything looks correct to me, > but I can't figure it out. > > I'm expecting the SQL statement to look like this > > select s.pk > from datadb.sample as s > where log(s.nsa_mstar) < 9; > > Any thoughts? Thanks. > > I can't see anything obviously wrong with your code, but it looks like Sample.nsa_logmstar is not actually resolving to the hybrid property in your query. What happens if you "print Sample.nsa_logmstar" just before the query? Otherwise, please provide a small runnable script that demonstrates the problem. Simon -- 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.