[sqlalchemy] Re: ORM and EXISTS?

2009-09-14 Thread Seppo11

On 11 Sep., 22:12, Conor  wrote:
> This query will get you close to your desired SQL:
> q = session.query(ATable)
> q = q.filter(
>     sa.exists(
>         [1],
>         ((BTable.atable_id == ATable.id)
>          & (CTable.cval.in_(["foo", "bar"]))),
>         from_obj=orm.join(BTable, CTable)))

Great! Works like a charm!

Thank you!
  seppo

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] ORM and EXISTS?

2009-09-10 Thread Seppo11

Hi!

I'm having trouble getting a query to work. The query looks rather
simple but I can't manage to build it using sqlalchemy - especially I
didn't find a way for the exists clause to build.

The query (simplified):

SELECT * from ATable WHERE EXISTS (
  SELECT 1 FROM BTable join CTable on BTable.CTable_id = CTable.id
WHERE CTable.cval IN ('foo', 'bar') AND BTable.ATable_id =
ATable.id
)

The Model (simplified):

class ATable(Base):
id = Column(Integer, primary_key=True)
aval = Column(Unicode(80))

class BTable(Base):
id = Column(Integer, primary_key=True)
atable_id  = Column(ForeignKey(ATable.id), index=True)
ctable_id  = Column(ForeignKey(CTable.id), index=True)
bval = Column(Unicode(80))

class CTable(Base):
id = Column(Integer, primary_key=True)
cval = Column(Unicode(80))


I tried a lot to get this query to work but nothing worked so far.
Optimistically I thought that something simple as

ex = session.query(BTable).join(CTable).filter(CTable.cval.in_(('foo',
'bar')))
q = session.query(ATable).filter(exists(ex))

could work "automagically" but that was unfortunately not the case.
Can anyone help me on this?

Thanks,
  seppo



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---