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')}]
"""

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

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"?

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