我修改了 Base 类以包含我所有的表都具有的三个默认列:
class Base(object):
id = Column(Integer, primary_key=True)
date_created = Column(DateTime, default=func.current_timestamp())
date_modified = Column(DateTime, default=func.current_timestamp(),
onupdate=func.current_timestamp())
我在两列之间有一对多的关系:
class User(Base):
__tablename__ = 'users'
name = Column(Text)
password = Column(Text)
items = relationship("Item", backref=
backref('user', order_by=date_modified),
cascade="all, delete, delete-orphan")
class Item(Base):
__tablename__ = 'items'
user_id = Column(Integer, ForeignKey('users.id'))
title = Column(Text)
如果我在每个表的类中明确定义了 date_created 和 date_modified 列,这曾经可以正常工作。但是,从 Base 继承时,它不起作用,并且出现以下错误:
NameError:名称“ date_modified ”未定义
如何使用order_by=column_from_mixin
(order_by=date_modified
)对反向引用关系进行排序?
谢谢你。