--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.---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+...@googlegroups.com.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/20210528122520.4gzwrf2w4b6cqno5%40x1.
On Fri, May 28, 2021 at 10:53:26AM -0400, Mike Bayer wrote:> the hybrid here is pulling in additional tables which are causing a cartesian product since there is nothing joining the Document entity to this other entity which seems to be DocumentTranslation. Run this in 1.4 and you will see the warnings generated. an ORDER BY etc. has to either produce a self-contained scalar expression, like a correlated subquery, or you otherwise need to make sure the query has JOIN/WHERE to link them together, such as:>> q = (> s.query(Document)> .join(DocumentTranslation)> .filter(Document.title.like("doc3%"))> .order_by(Document.title)> )
Thank you for your quick reply and help.It works with: .join(DocumentTranslation) or.join(Document.translations). However it doesn't work with.join(Document.current_translation), the FROM clause is wrong (as youcan see in my test_six()). This is because my Translation-like classesare polymorphic too I think.
>>> seems like you'd be better off making sure Document.title returns a correlated scalar subquery rather than what it does now which seems to be :>
yes, this would be really the best, but I don't see how could Isubselect from the .current_translation relationship (which is arelationship on an orm.aliased filtered by a primaryjoin). Ideally I'dlike to get something like:withq = (s.query(Document).join(Document.current_translation)
.filter(Document.title.like("doc3%")).order_by(Document.title))
I should have something like:SELECT (SELECT sub.title FROM sub) AS title, (SELECT sub.description FROM sub) AS descriptionFROMcontent cJOINdocument d ON c.id = d.content_idJOIN (SELECT(...)row_number() OVER (PARTITION BYdocument_translation.content_idORDER BYdocument_translation.language_id = 'fr' DESC,document_translation.language_id = 'en' DESC) AS "index"
FROMcontent_translationJOINdocument_translationON content_translation.language_id = document_translation.language_idAND content_translation.content_id = document_translation.content_id
WHEREdocument_translation.language_id IN ('fr', 'en')) AS subON sub.document_translation_content_id = content.idAND sub."index" = 1WHEREtitle LIKE 'doc3%'ORDER BYtitlebut I don't see any way in the hybrid property expression to subselectfrom the outer .current_translation query ..(I've updated my POC onJulien
> > To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com <mailto:sqlalchemy%2Bunsubscribe%40googlegroups.com>.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/20210528122520.4gzwrf2w4b6cqno5%40x1.> >>> --> SQLAlchemy -> The Python SQL Toolkit and Object Relational Mapper>>> To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.> ---> 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+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/fb68178f-16ac-45ec-b8c0-c1d5dde8b78a%40www.fastmail.com.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/20210614102045.vz5xgrkalxa5dil3%40x1.Attachments:
- poc.py