On Fri, Jan 15, 2016 at 6:16 AM, Brian Cherinka <havok2...@gmail.com> 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 '<Sample (pk={0},cube={1})'.format(self.pk,self.cube)
>
>    @hybrid_property
>    def nsa_logmstar(self):
>        try: return math.log10(self.nsa_mstar)
>        except ValueError as e:
>            return -9999.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.

Reply via email to