On 04/03/2017 03:19 PM, Colton Allen wrote:
|
class EntryModel(Model):

word_count = db.relationship(
        'WordCountModel', backref='entry', secondary='transcription',
        primaryjoin='and_('
        'WordCountModel.transcription_id == TranscriptionModel.id,'
        'TranscriptionModel.entry_id == EntryModel.id,'
        'TranscriptionModel.is_latest.is_(True))', viewonly=True)


query = session.query(EntryModel).outerjoin(aliased_word_count_model,
EntryModel.word_count)
print(query.all())  # ERROR

"""Error message.

HINT:  Perhaps you meant to reference the table alias "wordcount_1".
 [SQL: 'SELECT wordcount_1.id AS wordcount_1_id, wordcount_1.account_id
AS wordcount_1_account_id, wordcount_1.created_by_id AS
wordcount_1_created_by_id, wordcount_1.updated_by_id AS
wordcount_1_updated_by_id, wordcount_1.transcription_id AS
wordcount_1_transcription_id, wordcount_1.created_at AS
wordcount_1_created_at, wordcount_1.updated_at AS wordcount_1_updated_at
\nFROM entry LEFT OUTER JOIN (transcription AS transcription_1 JOIN
wordcount AS wordcount_1 ON transcription_1.id =
wordcount_1.transcription_id) ON wordcount.transcription_id =
transcription_1.id AND transcription_1.entry_id = entry.id AND
transcription_1.is_latest IS true \nWHERE entry.id IN (%(id_1)s)']
[parameters: {'id_1': UUID('c2a877a1-6140-4c7d-853d-11704ba502f3')}]
"""
|


The relationship() as built is almost certainly incorrect. You would not have a primaryjoin that spans across multiple tables and also includes "secondary".

First and foremost, sorry I didn't include functioning code.  I hope the
problem can be understood without it.

Unfortunately, beyond the fact that the relationship() definitely looks wrong, I can't provide further help without understanding what "transcription" (table) is as well as "TranscriptionModel", "WordCountModel" and aliased_word_count_model.

However, simple additions to primaryjoin/secondaryjoin need to fit one of a small number of possible models which are described at http://docs.sqlalchemy.org/en/latest/orm/join_conditions.html. The one here seems to most closely resemble simple use of "primaryjoin" in addition to "secondaryjoin". The WordCountModel part would be in "secondaryjoin", not "primaryjoin".




I think the primaryjoin where I reference "WordCountModel" is throwing
off my aliased join.  Is there a way to make a relative reference to
"WordCountModel"?

I don't know what "relative reference" means.


You can see the join condition is super messed up:

LEFT OUTER JOIN (transcription AS transcription_1 JOIN wordcount AS
wordcount_1 ON transcription_1.id = wordcount_1.transcription_id) ON
wordcount.transcription_id = transcription_1.id AND
transcription_1.entry_id = entry.id AND transcription_1.is_latest IS true

--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

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+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to