我有播放列表和曲目类,并为播放列表跟踪指定了外键。一切都好,当想要获得播放列表曲目我使用playlist.tracks。但现在我还需要获得播放列表的'id'跟踪。如何从关联表中获取“id”或任何额外列?
playlistsTracks = db.Table('tbl_test_playlisttracks',
db.Column('id', db.Integer, primary_key=True),
db.Column('playlist_id', db.Integer, db.ForeignKey('tbl_test_playlists.id', ondelete='CASCADE')),
db.Column('track_id', db.Integer, db.ForeignKey('tbl_tracks.id'))
)
class Track(db.Model):
__tablename__ = 'tbl_tracks'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
audio_url = db.Column(db.Text)
movie_id = db.Column(db.Integer)
entry = db.relationship('Playlist', secondary=playlistsTracks, backref=db.backref('tracks', lazy='dynamic'))
class Playlist(db.Model):
__tablename__ = 'tbl_test_playlists'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
user_id = db.Column(db.String(255))
current_rev = db.Column(db.Integer, default=0)
答案 0 :(得分:0)
db.session.query(playlistsTracks).join(Track).all()
或
db.session.query(playlistsTracks.c.id).join(Track).all()
其他实体的播放列表也是如此