我有两个对象,一个 Player 对象和一个 Team 对象。
我想在没有球员的情况下在数据库中找到所有 Team 对象并将其删除。
class Team(db.Model):
__tablename__ = 'Team'
id = db.Column(db.Integer, primary_key=True)
players = db.relationship('Player', backref='team', lazy='dynamic')
这是我迄今为止尝试过的(不成功):
all_teams = Team.query.all()
for a_team in all_teams:
if not a_team.players: # have also tried this with a .query.all() and a .all()
db.session.delete(a_team)
db.session.commit()
我究竟做错了什么?我应该如何解决这个问题?