Thursday 31 December 2020

Why am I unable to generate a query using relationships?

I'm experimenting with relationship functionality within SQLAlchemy however I've not been able to crack it. The following is a simple MRE:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, ForeignKey, Integer, create_engine
from sqlalchemy.orm import relationship, sessionmaker

Base = declarative_base()


class Tournament(Base):

    __tablename__ = "tournament"
    __table_args__ = {"schema": "belgarath", "extend_existing": True}

    id_ = Column(Integer, primary_key=True)
    tournament_master_id = Column(Integer, ForeignKey("belgarath.tournament_master.id_"))

    tournament_master = relationship("TournamentMaster", back_populates="tournament")


class TournamentMaster(Base):

    __tablename__ = "tournament_master"
    __table_args__ = {"schema": "belgarath", "extend_existing": True}

    id_ = Column(Integer, primary_key=True)
    tour_id = Column(Integer, index=True)

    tournament = relationship("Tournament", back_populates="tournament_master")


engine = create_engine("mysql+mysqlconnector://root:root@localhost/")
Session = sessionmaker(bind=engine)
session = Session()

qry = session.query(Tournament.tournament_master.id_).limit(100)

I was hoping to be able to query the id_ field from the tournament_master table through a relationship specified in the tournament table. However I get the following error:

AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Tournament.tournament_master has an attribute 'id_'

I've also tried replacing the two relationship lines with a single backref line in TournamentMaster:

tournament = relationship("Tournament", backref="tournament_master")

However I then get the error:

AttributeError: type object 'Tournament' has no attribute 'tournament_master'

Where am I going wrong?

(I'm using SQLAlchemy v1.3.18)



from Why am I unable to generate a query using relationships?

No comments:

Post a Comment