'"... missing FROM-clause entry for table..." error on simple SQLAlchemy join query?
Let's say I have a simple model:
class Entry(db.Model):
__tablename__ = "entries"
id = db.Column(db.BigInteger(), primary_key=True)
event_id = db.Column(db.BigInteger())
entry = db.Column(db.String(64), nullable=False)
... and I run a query like this:
from models import Entry
from sqlalchemy.orm import aliased
Comment = aliased(Entry)
answers = Entry.query.join(Comment, Comment.event_id==Entry.event_id) \
.with_entities(
Entry.event_id.label("entry_event_id"),
Comment.event_id.label("comment_event_id"),
Comment.entry.label("comment"),
Entry.entry.label("answer")
) \
.all()
This feels pretty straightforward, but I'm getting:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) missing FROM-clause entry for table "entries_2"
entries_2 here is just the aliased table. Does anyone have an idea of what's going wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
