有两个表,是多对多关系,分别叫account和resource,现在给定一组resource id,要求查出与每一个id都有关联的account,请问要怎么写。简化版数据表如下:
class Own(db.Model):
owner_id = db.Column(db.Integer, db.ForeignKey('account.id'),
primary_key=True)
owned_id = db.Column(db.Integer, db.ForeignKey('resource.id'),
primary_key=True)
count = db.Column(db.Integer, default=1)
owner = db.relationship('Account', back_populates='resources', lazy='joined')
owned = db.relationship('Resource', back_populates='owners', lazy='joined')
class Account(db.Model):`
id = db.Column(db.Integer, primary_key=True)
resources = db.relationship('Own', back_populates='owner', cascade='all')`
class Resource(db.Model):
id = db.Column(db.Integer, primary_key=True)
owners = db.relationship('Own', back_populates='owned', cascade='all')
————————
目前有个非常烂的想法,先用每个资源id在Own查出拥有此资源的owners,然后把这几组owners求交集得到用户