我有一个如下所示的数据库工作正常。现在我有一个名为Bob的用户拥有空间Mainspace。我想得到一个布尔值,看看他是否是该空间的拥有者。我尝试应用两个过滤器,但是我收到以下错误。
sqlalchemy.exc.InvalidRequestError: Can't compare a collection to an object or collection; use contains() to test for membership.
命令:
exists = Space.query.filter_by(name="Mainspace", owner="Bob").first()
数据库:
space_access = db.Table('space_access',
db.Column('userid', db.Integer, db.ForeignKey('user.id')),
db.Column('spaceid', db.Integer, db.ForeignKey('space.id')))
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(15), unique=True)
email = db.Column(db.String(50), unique=True)
password = db.Column(db.String(80))
role='admin';
spaces = db.relationship('Space', secondary=space_access, backref=db.backref('owner', lazy='dynamic'))
class Space(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True)
type = db.Column(db.String(50), unique=True)
答案 0 :(得分:4)
试试这个:
existing = User.query.join(User.spaces).filter(User.username=='Bob', Space.name=='Mainspace').first()
print(existing.id)
if existing != None:
print('Exists')