Thank you!Julien
--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 post to this group, send email to sqlal...@googlegroups.com.Visit this group at https://groups.google.com/group/sqlalchemy.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/16dddc84-d927-44e0-95dc-da9f579da733%40googlegroups.com.For more options, visit https://groups.google.com/d/optout.
On Tuesday, June 18, 2019 at 12:20:55 PM UTC+2, Julien Cigar wrote:Hello,I'm trying to add a 'read-only' relationship involving a subquery and I have some problems with remote and foreign sides, SQLAlchemy returns:sqlalchemy.exc.ArgumentError: Relationship Folder.current_translation could not determine any unambiguous local/remote column pairs based on join condition and remote_side arguments. Consider using the remote() annotation to accurately mark those elements of the join condition that are on the remote side of the relationship.I've copied my code here https://gist.github.com/silenius/e2c7fb393864b3f1fee8131336dd8b61Any idea what could be wrong ..?Basically I have a "translation" table and I'd like to JOIN that table for the current translation or the "fallback" language, so something like:select c.id, t.* from content c JOIN LATERAL (select ct.* from content_translation ct WHERE ct.content_id=c.id order by ct.language_id='fr' desc, ct.language_id='en' desc limit 1) as t ON t.content_id = c.id WHERE c.id=4855;Thank you!JulienI've taken another approach, as it looks like that I need something like https://docs.sqlalchemy.org/en/13/orm/join_conditions.html#row-limited-relationships-with-window-functionsIt works almost with: https://gist.github.com/silenius/b866232305e22b05e3f1f8705f4401a2 which produce something like https://gist.github.com/silenius/161c9a1263e7251e0213843598786640
partition = select([ ContentTranslation, func.row_number().over( order_by=[ (ContentTranslation.lang == 'fr').desc(), (ContentTranslation.lang == 'en').desc(), ], partition_by=ContentTranslation.content_id ).label('index') ]).alias()
The only remaining problem is that translation_cls (line 14 of first paste) is involved in a joined load inheritance scenario and that I'm getting a:sqlalchemy.exc.ProgrammingError: (psycopg2.errors.AmbiguousColumn) column reference "content_id" is ambiguouswhich is because SQLAlchemy doesn't alias properly in the subselect .. any idea how to fix this ?Thanks :)
--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 post to this group, send email to sqlal...@googlegroups.com.Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/fb55e73a-10d1-4a4f-b410-f614e2d8a736%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlal...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/8bfe074c-0ab4-4e71-8b43-afd90a6743aa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Attachments:
- trans.py
Thank you very much, it almost works !I have one minor issue, as translation_cls is involved in joined load inheritance the select([translation_cls], ...) results in:SELECTcontent_translation.language_id AS language_id,content_translation.content_id AS content_id,content_translation.title AS title,content_translation.description AS description,content_translation.fts AS fts,document_translation.language_id AS language_id,document_translation.content_id AS content_id,document_translation.body AS body,(...)which lead to a 2019-06-18 17:47:04.498 CEST [51817] ERROR: column reference "content_id" is ambiguous at character 3155for the foreign(_alias2.content_id) == content_cls.content_id,I could list and alias individual columns, but I wondered if SQLAlchemy could do this automatically ?
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/337cb354-4bd5-43f5-aee8-9ecce97a31f1%40googlegroups.com.
On Wed, Jun 19, 2019, at 4:48 AM, Julien Cigar wrote:Thank you very much, it almost works !I have one minor issue, as translation_cls is involved in joined load inheritance the select([translation_cls], ...) results in:SELECTcontent_translation.language_id AS language_id,content_translation.content_id AS content_id,content_translation.title AS title,content_translation.description AS description,content_translation.fts AS fts,document_translation.language_id AS language_id,document_translation.content_id AS content_id,document_translation.body AS body,(...)which lead to a 2019-06-18 17:47:04.498 CEST [51817] ERROR: column reference "content_id" is ambiguous at character 3155for the foreign(_alias2.content_id) == content_cls.content_id,I could list and alias individual columns, but I wondered if SQLAlchemy could do this automatically ?for the joined inheirtance, you need to compose the SELECT against both tables individually with the JOIN you want between them, I would probably use something likeselect([MyClass]).select_from(MyClass.__mapper__.selectable)
That's the (almost) final version if you're interrested: https://gist.github.com/silenius/568aca7545e1bc0400b53b6ec157807d
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/c54ac24d-933b-4d15-9991-5371bd8a28d3%40googlegroups.com.
On Wed, Jun 19, 2019, at 10:50 AM, Julien Cigar wrote:That's the (almost) final version if you're interrested: https://gist.github.com/silenius/568aca7545e1bc0400b53b6ec157807dgreat, I can't run it, so are you still getting any warnings about properties being overwritten ?
On Wednesday, June 19, 2019 at 9:53:42 PM UTC+2, Mike Bayer wrote:On Wed, Jun 19, 2019, at 10:50 AM, Julien Cigar wrote:That's the (almost) final version if you're interrested: https://gist.github.com/silenius/568aca7545e1bc0400b53b6ec157807dgreat, I can't run it, so are you still getting any warnings about properties being overwritten ?yes, sorry about that, I'm not that familiar with the Declarative API (I'm still using the mapper()))I get a bunch of SAWarning but it works as expected:2019-06-20 14:31:45,784 INFO [amnesia.translations][MainThread] SQLAlchemy after_configured handler _setup_translation called2019-06-20 14:31:45,784 DEBUG [amnesia.translations][MainThread] Adding translation properties: <class 'amnesia.modules.folder.model.Folder'> to <class 'amnesia.modules.folder.translations.model.FolderTranslation'>/usr/home/jcigar/code/venvs/amnesiabbpf/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py:1826: SAWarning: Property ContentTranslation.content on mapped class FolderTranslation->content_translation being replaced with new property FolderTranslation.content; the old property will be discarded% (self._props[key], self, prop)2019-06-20 14:31:45,807 DEBUG [amnesia.translations][MainThread] Adding translation properties: <class 'amnesia.modules.document.model.Document'> to <class 'amnesia.modules.document.translations.model.DocumentTranslation'>/usr/home/jcigar/code/venvs/amnesiabbpf/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py:1826: SAWarning: Property ContentTranslation.content on mapped class DocumentTranslation->document_translation being replaced with new property DocumentTranslation.content; the old property will be discarded% (self._props[key], self, prop)2019-06-20 14:31:45,816 DEBUG [amnesia.translations][MainThread] Adding translation properties: <class 'amnesia.modules.event.model.Event'> to <class 'amnesia.modules.event.translations.model.EventTranslation'>/usr/home/jcigar/code/venvs/amnesiabbpf/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py:1826: SAWarning: Property ContentTranslation.content on mapped class EventTranslation->event_translation being replaced with new property EventTranslation.content; the old property will be discarded% (self._props[key], self, prop)2019-06-20 14:31:45,825 DEBUG [amnesia.translations][MainThread] Adding translation properties: <class 'amnesia.modules.file.model.File'> to <class 'amnesia.modules.file.translations.model.FileTranslation'>/usr/home/jcigar/code/venvs/amnesiabbpf/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py:1826: SAWarning: Property ContentTranslation.content on mapped class FileTranslation->content_translation being replaced with new property FileTranslation.content; the old property will be discarded% (self._props[key], self, prop)
If you'd like to add this configuration to the SQLAlchemy test cases I could spend some time to make a clean one .. ?
Also, I could also write some doc to add this configuration to the "Row-Limited Relationships with Window Functions" if you think it's worthwhile ..?
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/6139daed-fe74-479d-813c-5884ff7b01a5%40googlegroups.com.
On Thu, Jun 20, 2019, at 8:39 AM, Julien Cigar wrote:On Wednesday, June 19, 2019 at 9:53:42 PM UTC+2, Mike Bayer wrote:On Wed, Jun 19, 2019, at 10:50 AM, Julien Cigar wrote:That's the (almost) final version if you're interrested: https://gist.github.com/silenius/568aca7545e1bc0400b53b6ec157807dgreat, I can't run it, so are you still getting any warnings about properties being overwritten ?yes, sorry about that, I'm not that familiar with the Declarative API (I'm still using the mapper()))I get a bunch of SAWarning but it works as expected:2019-06-20 14:31:45,784 INFO [amnesia.translations][MainThread] SQLAlchemy after_configured handler _setup_translation called2019-06-20 14:31:45,784 DEBUG [amnesia.translations][MainThread] Adding translation properties: <class 'amnesia.modules.folder.model.Folder'> to <class 'amnesia.modules.folder.translations.model.FolderTranslation'>/usr/home/jcigar/code/venvs/amnesiabbpf/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py:1826: SAWarning: Property ContentTranslation.content on mapped class FolderTranslation->content_translation being replaced with new property FolderTranslation.content; the old property will be discarded% (self._props[key], self, prop)2019-06-20 14:31:45,807 DEBUG [amnesia.translations][MainThread] Adding translation properties: <class 'amnesia.modules.document.model.Document'> to <class 'amnesia.modules.document.translations.model.DocumentTranslation'>/usr/home/jcigar/code/venvs/amnesiabbpf/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py:1826: SAWarning: Property ContentTranslation.content on mapped class DocumentTranslation->document_translation being replaced with new property DocumentTranslation.content; the old property will be discarded% (self._props[key], self, prop)2019-06-20 14:31:45,816 DEBUG [amnesia.translations][MainThread] Adding translation properties: <class 'amnesia.modules.event.model.Event'> to <class 'amnesia.modules.event.translations.model.EventTranslation'>/usr/home/jcigar/code/venvs/amnesiabbpf/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py:1826: SAWarning: Property ContentTranslation.content on mapped class EventTranslation->event_translation being replaced with new property EventTranslation.content; the old property will be discarded% (self._props[key], self, prop)2019-06-20 14:31:45,825 DEBUG [amnesia.translations][MainThread] Adding translation properties: <class 'amnesia.modules.file.model.File'> to <class 'amnesia.modules.file.translations.model.FileTranslation'>/usr/home/jcigar/code/venvs/amnesiabbpf/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py:1826: SAWarning: Property ContentTranslation.content on mapped class FileTranslation->content_translation being replaced with new property FileTranslation.content; the old property will be discarded% (self._props[key], self, prop)What happens if you don't overwrite the "content" relationship each time and just leave the base one in place? the pattern you are doing there is not one that has explicit support. IIRC the "content" relationship you are adding looks just like the one that's there, except it is against more specific subclasses. I can see how that is valuable but it shouldn't have any real-world consequence...however if it does, like it is making lazyloads more specific perhaps, then I might want to refine that warning to allow for more specific relationships against subclasses.
Just to add that it works like a charm, even with recursive queries.I have a classical parent->child relationship (website structure) and Iam able to retrieve the whole structure, including translations (withfallback on a default one) in just _one_ query, this is really amazing:)and SQLAlchemy generates https://gist.github.com/silenius/2f9df00575daab6d009e43dff92a3902
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/20190625150309.GE92534%40home.lan.
For more options, visit https://groups.google.com/d/optout.
Attachments:
- signature.asc