[sqlalchemy] Query with outer join and function

2009-04-10 Thread Joril
Hi everyone! I'm trying to concoct a somewhat complicated query via Query API.. The situation is the following: - A many-to-one relation between classes C and D - Class C has an attribute value My objective is to retrieve the ids of a left outer join between D and C (so, all the Ds and

[sqlalchemy] How to get inserted ids after row insert?

2009-04-10 Thread Andrija Frincic aka BobRock
Hi all I use insert statement as text to insert record into MySQL DB. I chose this method because I use INSERT in combination with SELECT, so there is no way to use SQL Expression language. My problem is that there is no inserted ids available after inserting data using raw SQL statement. When I

[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread az
that's what i have in bitemporal queries. u need a groupby and subquery/ies. something along subs = select( [C.id.label('cid'), C.d_id.label('did'), func.max(C.value).label('cvalue')] ).group_by( C.id ) giving the max cid/cvalues, and then somehow join Ds with that. D.query(

[sqlalchemy] Translating complex query into SQLA

2009-04-10 Thread Marcin Krol
Hello everyone? I need to translate following complex query into SQLA. How do I do that? SELECT h.id AS hosts_id, h.ip AS hosts_ip, h.hostname AS hosts_hostname, h.location AS hosts_location, h.architecture_id AS hosts_architecture_id, h.os_kind_id AS hosts_os_kind_id, h.os_version_id AS

[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread Joril
On 10 Apr, 12:52, a...@svilendobrev.com wrote: that's what i have in bitemporal queries. u need a groupby and subquery/ies. I see, thanks for your hint! I tried to do it with bare SQL via pgadmin, and I ended up with select d.id, c.id from d left outer join ( select c.* from c join (

[sqlalchemy] Intermittent error ProgrammingError: (ProgrammingError) current transaction is aborted, commands ignored until end of transaction block

2009-04-10 Thread Marcin Krol
Hello everyone, From time to time I get this error when processing my own query in from_statement: ProgrammingError: (ProgrammingError) current transaction is aborted, commands ignored until end of transaction block \nSELECT h.id AS hosts_id, h.ip AS hosts_ip, h.hostname AS

[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread Mike Conley
Try this, has one nested query sub = session.query(C.id.label('c_id'), C.d_id.label('d_id'), func.max(C.value).label('c_maxvalue') ).group_by(C.d_id).subquery() q = session.query(D.id,sub.c.c_id,sub.c.c_maxvalue).outerjoin(sub) print q for row in q: print 'D-id:%s

[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread Mike Conley
This will teach me to run a test it first, I don't think this is exactly right, but it should be close. -- Mike Conley On Fri, Apr 10, 2009 at 9:43 AM, Mike Conley mconl...@gmail.com wrote: Try this, has one nested query sub = session.query(C.id.label('c_id'), C.d_id.label('d_id'),

[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread Joril
On 10 Apr, 15:43, Mike Conley mconl...@gmail.com wrote: Try this, has one nested query sub = session.query(C.id.label('c_id'),         C.d_id.label('d_id'),         func.max(C.value).label('c_maxvalue')         ).group_by(C.d_id).subquery() I tried something like that earlier, but

[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread Joril
On 10 Apr, 15:54, Mike Conley mconl...@gmail.com wrote: This will teach me to run a test it first, I don't think this is exactly right, but it should be close. That's ok all the same, thanks for taking the time for posting :) Anyway my current implementation via Query API is sub =

[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread Joril
On 10 Apr, 16:24, Joril jor...@gmail.com wrote: Now let's see if I can fetch complete instances of C instead of just the id X-) For the record, my final implementation: sub = s.query(C.d_id, func.max(C.value).label(v)). group_by(C.d_id).subquery() sub2 = s.query(C).join((sub,

[sqlalchemy] Re: How to get inserted ids after row insert?

2009-04-10 Thread Michael Bayer
call result.lastrowid. you'll get whatever the DBAPI deems worthy of sending. Andrija Frincic aka BobRock wrote: Hi all I use insert statement as text to insert record into MySQL DB. I chose this method because I use INSERT in combination with SELECT, so there is no way to use SQL

[sqlalchemy] Re: Translating complex query into SQLA

2009-04-10 Thread Michael Bayer
SQLA can execute your query as text. For an existing query like this, I'd stick with that unless you require DB-neutral behavior or other malleability. The constructs used would be the standard Table objects, join objects, etc. all described in the SQL Expression Tutorial. Marcin Krol wrote:

[sqlalchemy] Re: Intermittent error ProgrammingError: (ProgrammingError) current transaction is aborted, commands ignored until end of transaction block

2009-04-10 Thread Michael Bayer
Marcin Krol wrote: Hello everyone, From time to time I get this error when processing my own query in from_statement: ProgrammingError: (ProgrammingError) current transaction is aborted, commands ignored until end of transaction block a previous statement failed within the transaction

[sqlalchemy] Re: Intermittent error ProgrammingError: (ProgrammingError) current transaction is aborted, commands ignored until end of transaction block

2009-04-10 Thread Marcin Krol
Michael Bayer wrote: From time to time I get this error when processing my own query in from_statement: ProgrammingError: (ProgrammingError) current transaction is aborted, commands ignored until end of transaction block a previous statement failed within the transaction and must be

[sqlalchemy] 5.2 to 5.3 base-class/sub-class handling change

2009-04-10 Thread limscoder
In 5.2 I was able to map a base-class, and sub-classes would automatically be persisted as the base type. In 5.3 I get an UnmappedInstanceError when trying to persist an object of the sub- class type. I didn't see anything about this change in the changelog. Is there a way to get the 5.3

[sqlalchemy] TypeDecorator class as primary key

2009-04-10 Thread warp
Hello, I wanted to use a custom type as an auto-increment / primary key in one of my tables. The type is made like this: class BoxId (sqlalchemy.types.TypeDecorator): impl = sqlalchemy.types.Integer Using it in a table like this: box = Table ('box', metadata, Column ('id',

[sqlalchemy] Re: 5.2 to 5.3 base-class/sub-class handling change

2009-04-10 Thread Michael Bayer
limscoder wrote: In 5.2 I was able to map a base-class, and sub-classes would automatically be persisted as the base type. In 5.3 I get an UnmappedInstanceError when trying to persist an object of the sub- class type. I didn't see anything about this change in the changelog. Is there a way

[sqlalchemy] Re: TypeDecorator class as primary key

2009-04-10 Thread Michael Bayer
warp wrote: Hello, I wanted to use a custom type as an auto-increment / primary key in one of my tables. The type is made like this: class BoxId (sqlalchemy.types.TypeDecorator): impl = sqlalchemy.types.Integer Using it in a table like this: box = Table ('box', metadata,