【发布时间】:2024-11-14 03:20:02
【问题描述】:
我有这些模型:
class User(UserMixin, db.Model):
__tablename__ = 'users_user'
...
country = db.Column(db.Integer, db.ForeignKey('countries.id'))
class Country(db.Model):
__tablename__ = 'countries'
id = db.Column(db.Integer, primary_key=True)
...
user_country = db.relationship('User', backref='user_country', lazy='joined')
我正在尝试这个查询:
User.query.options(joinedload(Country.user_country)).filter_by(id=current_user.get_id()).first()
这会抛出这个错误:
ArgumentError: Can't find property 'user_country' on any entity specified in this Query.
Note the full path from root (Mapper|User|users_user) to target entity must be specified.
这里有什么问题?
【问题讨论】:
-
为什么不直接使用
current_user.user_country?
标签: python orm flask sqlalchemy flask-sqlalchemy