Re: [sqlalchemy] Re: Dynamically constructing joins

2015-03-25 Thread Greg Silverman
Ha! Ha! On my previous attempts, I had something similar to this, but
instead, I had

query = db.session.query(label('sid',
 distinct(a[1].c.patient_sid)))

if (n  1):
for table in join_tables[1:]:
for criterion in join_criteria[1:]:
query = query.join(eval(table), eval(criterion))

Where the variables table and criterion were built lists, so that I ended
up doing a Cartesian product of all my tables, which was giving me many
problems, with aliasing being the least of it!

Thanks!

Greg--

On Tue, Mar 24, 2015 at 11:22 PM, Jonathan Vanasco jonat...@findmeon.com
wrote:

 any reason why you're not building a query like this?

query = db.session.query(label('sid',
  distinct(a[1].c.patient_sid)))
if n = 2
   query = query.\
 join(a[2],a[2].c.patient_sid==a[1].c.patient_sid)
if n = 3
   query = query.\
 join(a[3],a[3].c.patient_sid==a[1].c.patient_sid)

 or

query = db.session.query(label('sid',
  distinct(a[1].c.patient_sid)))
for i in range(2, n):
   query = query.\
 join(a[i],a[i].c.patient_sid==a[1].c.patient_sid)


  --
 You received this message because you are subscribed to a topic in the
 Google Groups sqlalchemy group.
 To unsubscribe from this topic, visit
 https://groups.google.com/d/topic/sqlalchemy/SySyi4CCCUY/unsubscribe.
 To unsubscribe from this group and all its topics, 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.




-- 
Greg M. Silverman
Senior Developer Analyst
Cardiovascular Informatics http://www.med.umn.edu/cardiology/
University of Minnesota
612-626-0919
g...@umn.edu

 ›  flora-script http://flora-script.grenzi.org/ ‹
 ›  grenzi.org  ‹
 ›  evaluate-it.org  ‹

-- 
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.


[sqlalchemy] Re: Dynamically constructing joins

2015-03-25 Thread Jonathan Vanasco
Yeah, there's no reason to touch eval -- and a lot of reasons not to. 
 Security issues aside, when you make a mistake the error will be 
completely unintelligible.

You can create joins dynamically very easily by just iteratively building 
up on it, and using getattr() if needed.

If you're doing any advanced things (subqueries, aliases, etc), I would 
suggest keeping the online docs loaded in a browser window and paying close 
attention to the return values.  Most operations will return a query, but a 
few will return another object.

-- 
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.


[sqlalchemy] Re: Dynamically constructing joins

2015-03-25 Thread Horcle
eval() was definitely not doing what I expected. Thanks for the tip about 
getattr(), and thanks for helping get my head screwed on right!

Greg--

On Wednesday, March 25, 2015 at 11:33:44 AM UTC-5, Jonathan Vanasco wrote:

 Yeah, there's no reason to touch eval -- and a lot of reasons not to. 
  Security issues aside, when you make a mistake the error will be 
 completely unintelligible.

 You can create joins dynamically very easily by just iteratively building 
 up on it, and using getattr() if needed.

 If you're doing any advanced things (subqueries, aliases, etc), I would 
 suggest keeping the online docs loaded in a browser window and paying close 
 attention to the return values.  Most operations will return a query, but a 
 few will return another object.


-- 
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.


[sqlalchemy] Re: Dynamically constructing joins

2015-03-24 Thread Jonathan Vanasco
any reason why you're not building a query like this?

   query = db.session.query(label('sid',
 distinct(a[1].c.patient_sid)))
   if n = 2
  query = query.\
join(a[2],a[2].c.patient_sid==a[1].c.patient_sid)
   if n = 3
  query = query.\
join(a[3],a[3].c.patient_sid==a[1].c.patient_sid)

or

   query = db.session.query(label('sid',
 distinct(a[1].c.patient_sid)))
   for i in range(2, n):
  query = query.\
join(a[i],a[i].c.patient_sid==a[1].c.patient_sid)


-- 
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.