[英]Flask SqlAlchemy join two models without foreign key MYSQL
I am joining two models without a foreign key:我加入了两个没有外键的模型:
Models:楷模:
class Users(db.Model):
__tablename__ = "Users"
userName = db.Column(db.String, primary_key=True)
lastLogin = db.Column(db.DateTime)
class TimeOff
__tablename__ = "timeOff"
timeOffID = db.Column(db.Integer, primary_key=True)
userName = db.Column("userName", db.String, db.ForeignKey('appUsers.userName')),
dayWork = db.Column(db.DateTime)
View:看法:
result = db.session.query(models.Users).join(models.TimeOff)
sqlalchemy.exc.InvalidRequestError: Could not find a FROM clause to join from.
sqlalchemy.exc.InvalidRequestError: 找不到要加入的 FROM 子句。 Tried joining to but got: Can't find any foreign key relationships between 'TimeOff' and 'Users'.
尝试加入但得到:找不到“TimeOff”和“Users”之间的任何外键关系。
I dont have a foreign key defined in table我没有在表中定义外键
You need to tell SQLAlchemy how to join the tables.您需要告诉 SQLAlchemy 如何连接表。 Try something like this:
尝试这样的事情:
result = db.session.query(Users).join(TimeOff,Users.userName==TimeOff.userName)
To improve upon @Matt Healy's answer, if you also want to be able to access attributes on the joined object you can do something like:为了改进@Matt Healy 的回答,如果您还希望能够访问连接对象的属性,您可以执行以下操作:
user, timeOff = db.session.query(Users, TimeOff).join(
TimeOff, Users.userName == TimeOff.userName
).first()
Then timeOff.dayWork
etc. will give the information you need.然后
timeOff.dayWork
等将提供您需要的信息。
Its an old post but I had a similar problem这是一个旧帖子,但我遇到了类似的问题
result = session.query(models.Users).join(models.TimeOff, models.Users.userName == models.TimeOff.userName).all()
with this method, I can reach the features of the first object which is Users but not the TimeOff.使用这种方法,我可以达到第一个对象的功能,即用户而不是 TimeOff。 I am wondering if it is possible to reach the secondary object's attributes.
我想知道是否有可能达到次要对象的属性。 But I hope this helps.
但我希望这会有所帮助。
This worked for me, I have 3 tables, And I have the rtf_id which is unique to all three tables, you have to use the select_from keyword which tells the table starting from left.这对我有用,我有 3 个表,并且我有所有三个表都唯一的 rtf_id,你必须使用 select_from 关键字来告诉表从左边开始。
db_data = dbobj.session.query(A, B, C). \
select_from(A).join(B, B.rtf_id == A.rtf_id). \
join(C, C.rtf_id == A.rtf_id).all()
In my situation, I want to join the two objects so that I can get the Author
information of the Posts
, but I don't want to use the architecture-level foreign key design to associate the two tables.here is my codes:在我的情况下,我想加入两个对象,以便我可以获取
Posts
的Author
信息,但我不想使用架构级外键设计来关联两个表。这里是我的代码:
class Posts(db.Model):
id= db.Column(db.String, primary_key=True)
author_code= db.Column(db.String)
...
class Author:
id= db.Column(db.Integer, primary_key=True)
name= db.Column(db.String),
code= db.Column(db.String)
address= db.Column(db.String)
...
we can add the code like this:我们可以添加这样的代码:
from sqlalchemy.orm import relationship
class Posts(db.Model):
id= db.Column(db.String, primary_key=True)
author_code= db.Column(db.String)
...
# add this line
author_info = relationship('Author',
foreign_keys=[author_code],
primaryjoin='Author.code == Posts.author_code')
then we can get author_info
by query like this:然后我们可以通过
author_info
的查询获取author_info
:
post_id = xxx # some id you defined
post = Posts.query.filter_by(id=post_id).one_or_none()
# here you can get `author_info` attributes you want.
print(dir(post),post.author_info)
You can learn more about this with link here ,and read the documents Configuring how Relationship Joins — SQLAlchemy 1.4 Documentation您可以通过此处的链接了解有关此内容的更多信息,并阅读文档配置关系连接的方式 — SQLAlchemy 1.4 文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.