On Aug 3, 2012, at 3:22 PM, kris wrote:

> SA 0.7.8
> I am getting some strange results when using a subquery that returns  
> Duplicate
> rows.  Note in line 7 I am not using distinct, yet I get one object where I 
> would expect
> 2.  Also if I  query.count I do actually receive 2.  
> What's going on here?
> 
> Thanks,
> kris
> 
> vq1 = DBSession.query(Value).filter_by(document_id = 622849) 
> 
> In [3]: vq1.all()
> Out[3]: 
> [<bq.data_service.model.tag_model.Value object at 0x59a2890>,
>  <bq.data_service.model.tag_model.Value object at 0x59a2990>]
> 
> In [5]: sq1 = vq1.with_labels().subquery()
> In [6]: q = DBSession.query (Taggable).filter (Taggable.id == 
> sq1.c.values_valobj)
> 
> In [7]: q.all()
> Out[7]: [<bq.data_service.model.tag_model.Taggable object at 0x59b0590>]
> 
> In [9]: q.count()
> Out[9]: 2L
> 
> From sql:
> 
> select * from values where resource_parent_id = 622849;
>  resource_parent_id | indx | valstr | valnum | valobj | document_id 
> --------------------+------+--------+--------+--------+-------------
>              622849 |    0 |        |        | 622840 |      622849
>              622849 |    1 |        |        | 622840 |      622849
> 
> 
> =# select id  from taggable, (select * from values where document_id = 
> 622849) as v where taggable.id = v.valobj;

.all() when used to return ORM instances will de-duplicate based on primary key 
identity as those instances come in.  this is part of how joined eager loading 
manages to return only one instance of each parent object even if it comes back 
as many rows due to joins.




>    id   
> --------
>  622840
>  622840
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/sqlalchemy/-/Y6KJNVstnSYJ.
> 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.

Reply via email to