[sqlalchemy] select distinct on a relation to an entity with composite primary key

2011-01-20 Thread NiL
hi list,

I have the following (elixir) definitions

class Invoice(Entity):
user_name = Field(Unicode(255))
item = ManyToOne(Item)

class Item(Entity):
item_id = Field(Integer,  primary_key=True)
service_id = Field(Unicode(255),  primary_key=True)
item_class = Field(Unicode(255),  primary_key=True)

I wish to select invoices that have distinct items

(Pdb) Session.query(Invoice.item.distinct())
*** RuntimeError: maximum recursion depth exceeded while calling a
Python object

this works
(Pdb) Session.query(Invoice.item_item_id.distinct())
sqlalchemy.orm.query.Query object at 0x988e7ec
but doesn't lead to the expected result

here is the invoice's table description

CREATE TABLE invoice (
id INTEGER NOT NULL,
user_name VARCHAR(255),
item_item_id INTEGER,
item_service_id VARCHAR(255),
item_item_class VARCHAR(255),
PRIMARY KEY (id),
CONSTRAINT invoice_item_item_id_item_service_id_item_item_class_fk
FOREIGN KEY(item_item_id, item_service_id, item_item_class) REFERENCES
item (item_id, service_id, item_class)
)


How can I achieve my query ?

regards
NIL

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



Re: [sqlalchemy] select distinct on a relation to an entity with composite primary key

2011-01-20 Thread Michael Bayer
its a little unfortunate that Invoice.item.distinct() is recursion overflowing 
like that, but in general if you want to select distinct Item objects you'd be 
saying query(Item).distinct().

not sure I understand what invoices that have distinct items means.   I 
understand invoices that have items.   If Invoice inv1 and inv2 both 
reference item im1, are you saying you don't want either of them ?  Only 
invoice inv3, which references im2, and nobody else references im2 ?

thats a fairly tricky query, probably using EXISTS:

from sqlalchemy.orm import aliased
from sqlalchemy import and_

inv = aliased(Invoice)
query(Invoice).filter(~Invoice.any(and_(Invoice.item_id==inv.item_id, 
Invoice.id!=inv.id)))



On Jan 20, 2011, at 6:33 AM, NiL wrote:

 hi list,
 
 I have the following (elixir) definitions
 
 class Invoice(Entity):
user_name = Field(Unicode(255))
item = ManyToOne(Item)
 
 class Item(Entity):
item_id = Field(Integer,  primary_key=True)
service_id = Field(Unicode(255),  primary_key=True)
item_class = Field(Unicode(255),  primary_key=True)
 
 I wish to select invoices that have distinct items
 
 (Pdb) Session.query(Invoice.item.distinct())
 *** RuntimeError: maximum recursion depth exceeded while calling a
 Python object
 
 this works
 (Pdb) Session.query(Invoice.item_item_id.distinct())
 sqlalchemy.orm.query.Query object at 0x988e7ec
 but doesn't lead to the expected result
 
 here is the invoice's table description
 
 CREATE TABLE invoice (
   id INTEGER NOT NULL,
   user_name VARCHAR(255),
   item_item_id INTEGER,
   item_service_id VARCHAR(255),
   item_item_class VARCHAR(255),
   PRIMARY KEY (id),
   CONSTRAINT invoice_item_item_id_item_service_id_item_item_class_fk
 FOREIGN KEY(item_item_id, item_service_id, item_item_class) REFERENCES
 item (item_id, service_id, item_class)
 )
 
 
 How can I achieve my query ?
 
 regards
 NIL
 
 -- 
 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.
 

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