I've noticed that my query is getting all data from my joined tables but I can only read that data when I specifically try and access it.
My unedited file:
query = db.session.query(Rating, Song).filter(Rating.id==Song.id).all()
print(query) #<----- This prints [(4.75, MMMBop), (3.00, bombastic)]
for x in query:
print(f"{x[1].title}:{x[1].artist}:{x[1].released}") #<-- This prints MMMBop:Hansons:1997\nbombastic:shaggy:1995
Why is this?
EDIT
I have added my model now. repr was the first thing I checked and I have run the code again after a reboot so there can't be any variables lurking. No repr is even including the artist and release.
from application import db
association_table = db.Table('association',
db.Column('songs_id', db.Integer,
db.ForeignKey('songs.id')),
db.Column('genres_id', db.Integer,
db.ForeignKey('genres.id'))
)
class Rating(db.Model):
__tablename__ = 'songs_ratings'
id = db.Column(db.Integer, primary_key=True)
rating = db.Column(db.Numeric(precision=3, scale=2),
index=True, nullable=False)
def __repr__(self):
return '{}'.format(self.rating)
class Song(db.Model):
__tablename__ = 'songs'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), index=True, unique=True, nullable=False)
artist = db.Column(db.String(30), primary_key=False,
unique=False, nullable=False)
release = db.Column(db.Date, nullable=False)
genres = db.relationship(
"Genre", secondary=association_table, backref=db.backref('songs'))
def __repr__(self):
return '{}'.format(self.title)
class Genre(db.Model):
__tablename__ = 'genres'
id = db.Column(db.Integer, primary_key=True)
category = db.Column(db.String(80), index=True,
unique=True, nullable=False)
def __repr__(self):
return '{}'.format(self.category)