我正在使用带有 sqlalchemy 的 Flask 框架,我无法查询结果的一些相关表。
用户 > 有很多 > 订单
所以我有订单,我想查询该订单所属的用户。
order = Order.query.get( id )
return order.user()
当我调用它时,我发现“用户”对象不可调用。这是我的关系的定义方式:
# ----------------------------------------------------------------------------------
# MODELS
# ----------------------------------------------------------------------------------
class User( object ):
__tablename__ = 'user'
query = db_session.query_property()
class Order( object ):
__tablename__ = 'order'
query = db_session.query_property()
# ----------------------------------------------------------------------------------
# MODELS SCHEMA
# ----------------------------------------------------------------------------------
users_table = Table( 'user', metadata,
Column( 'id', Integer, primary_key = True ),
Column( 'username', String( 50 ), unique = True ),
Column( 'email', String( 50 ), unique = True ),
Column( 'password', String( 32 ), nullable = False ),
...
mysql_engine = 'InnoDB',
mysql_charset = 'utf8'
)
orders_table = Table( 'order', metadata,
Column( 'id', Integer, primary_key = True ),
Column( 'user_id', Integer, ForeignKey( 'user.id' ) ),
Column( 'transaction_id', String( 32 ), unique = True ),
...
mysql_engine = 'InnoDB',
mysql_charset = 'utf8'
)
# ----------------------------------------------------------------------------------
# MAPPERS
# ----------------------------------------------------------------------------------
mapper( User, users_table, properties = {
# has many
'orders' : relationship( Order )
} )
mapper( Order, orders_table, properties = {
# belongs to
'user': relationship( User )
} )
知道为什么会出现这个错误吗?