MathiasRa
MathiasRa

Reputation: 835

SQLalechemy query returns object instead of value

I want to get all the values from the row for a query, however when I run the below code it returns an object instead of the values:

Session = sessionmaker()
Session.configure(bind=engine)  #
session = Session()

class Player(Base):
    __tablename__ = 'player_alc'
    id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
    name = Column(String(50))
    country= Column(String(50))
    first_name = Column(String(50))
    last_name= Column(String(50))


class Player_Mapping(Base):
    __tablename__ = 'player_external_id_mapping_alc'
    external_id = Column(String(50), nullable=False, primary_key=True)
    external_source_name = Column(String(50), nullable=False, primary_key=True)
    id = Column(Integer, ForeignKey('player_alc.id'))
    Player = relationship("Player", back_populates="player_mapping")

Player.player_mapping = relationship(
"Player_Mapping",  back_populates="Player")


query= session.query(Player_Mapping).filter_by(external_id=external_id, external_source_name=external_source_name).one()

Above code returns the following value for the query:

ORM_classes.Player_Mapping object at 0x0000016119E24D30>

Upvotes: 0

Views: 2032

Answers (1)

Mahi
Mahi

Reputation: 21941

You are asking for the object, right here:

session.query(Player_Mapping)

You can access all the fields through the returned object though, for example query.Player or query.external_source_name.

If you want to explicitly get the columns rather than an object, you can use:

session.query(Player_Mapping.Player, Player_Mapping.external_source_name)

Upvotes: 2

Related Questions