根据另一个 table 的值从一个 table 中选择数据
Selecting data from one table based on the value of another
科目 Table:
id | account_id | name
-------------------------
8 | 112 | Biology
主题Table:
id | subject_id | name
-------------------------
1 | 8 | Plants
使用 Flask-SQLAlchemy,我想根据 Subjects
中 account_id
的值 select 来自 Topics
table 的一行] table.
我有一个 account_id
的列表,如下所示:users = [139, 193, 112, 028,]
并且想要 select 这些用户的主题。
我试过这个:
topicQuery = db.session.query(Topic).filter(Topic.Subject.has(Topic.Subject.accountID.in_(usersID))).all()
但收到错误 AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Topic.Subject has an attribute 'accountID'
我认为这是我如何定义 Models.py
文件的问题,该文件如下所示:
class Subject(db.Model):
__tablename__ = 'subjects'
id = db.Column(db.Integer, primary_key=True)
accountID = db.Column(db.Integer, db.ForeignKey('accounts.id'))
name = db.Column(db.String(100))
Topic = db.relationship('Topic', backref=backref('Subject', cascade='delete'))
def __init__(self, accountID, name):
self.accountID = accountID
self.name = name
class Topic(db.Model):
__tablename__ = 'topics'
id = db.Column(db.Integer, primary_key=True)
subjectID = db.Column(db.Integer, db.ForeignKey('subjects.id', onupdate='CASCADE', ondelete='CASCADE'))
name = db.Column(db.String(150))
def __init__(self, subjectID, name):
self.subjectID = subjectID
self.name = name
那么为什么我会收到这个错误,我该如何解决这个问题?谢谢。
要么从 Topic.Subject.accountID.in_(usersID)
中删除多余的 Topic.
:
topicQuery = (
db.session.query(Topic)
.filter(Topic.Subject.has(Subject.accountID.in_(usersID)))
.all()
)
或者,做一个简单的连接查询,这已经足够了(而且可能更快):
topicQuery = (
db.session.query(Topic)
.join(Subject).filter(Subject.accountID.in_(usersID))
.all()
)
科目 Table:
id | account_id | name
-------------------------
8 | 112 | Biology
主题Table:
id | subject_id | name
-------------------------
1 | 8 | Plants
使用 Flask-SQLAlchemy,我想根据 Subjects
中 account_id
的值 select 来自 Topics
table 的一行] table.
我有一个 account_id
的列表,如下所示:users = [139, 193, 112, 028,]
并且想要 select 这些用户的主题。
我试过这个:
topicQuery = db.session.query(Topic).filter(Topic.Subject.has(Topic.Subject.accountID.in_(usersID))).all()
但收到错误 AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Topic.Subject has an attribute 'accountID'
我认为这是我如何定义 Models.py
文件的问题,该文件如下所示:
class Subject(db.Model):
__tablename__ = 'subjects'
id = db.Column(db.Integer, primary_key=True)
accountID = db.Column(db.Integer, db.ForeignKey('accounts.id'))
name = db.Column(db.String(100))
Topic = db.relationship('Topic', backref=backref('Subject', cascade='delete'))
def __init__(self, accountID, name):
self.accountID = accountID
self.name = name
class Topic(db.Model):
__tablename__ = 'topics'
id = db.Column(db.Integer, primary_key=True)
subjectID = db.Column(db.Integer, db.ForeignKey('subjects.id', onupdate='CASCADE', ondelete='CASCADE'))
name = db.Column(db.String(150))
def __init__(self, subjectID, name):
self.subjectID = subjectID
self.name = name
那么为什么我会收到这个错误,我该如何解决这个问题?谢谢。
要么从 Topic.Subject.accountID.in_(usersID)
中删除多余的 Topic.
:
topicQuery = (
db.session.query(Topic)
.filter(Topic.Subject.has(Subject.accountID.in_(usersID)))
.all()
)
或者,做一个简单的连接查询,这已经足够了(而且可能更快):
topicQuery = (
db.session.query(Topic)
.join(Subject).filter(Subject.accountID.in_(usersID))
.all()
)