Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将backref(..,order_by=..)中mixin类的列名与SQLAlchemy一起使用?_Python_Sqlite_Python 2.7_Sqlalchemy - Fatal编程技术网

Python 如何将backref(..,order_by=..)中mixin类的列名与SQLAlchemy一起使用?

Python 如何将backref(..,order_by=..)中mixin类的列名与SQLAlchemy一起使用?,python,sqlite,python-2.7,sqlalchemy,Python,Sqlite,Python 2.7,Sqlalchemy,我修改了基类,使其包含所有表都具有的三个默认列: 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(),

我修改了基类,使其包含所有表都具有的三个默认列:

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\u created和date\u modified列,那么这种方法可以很好地工作。但是,当从基继承时,它不起作用,我得到以下错误:

名称错误:未定义名称“修改日期”

如何使用mixin中的
order\u by=column\u对backref关系进行排序(
order\u by=date\u modified


谢谢。

您可以使用以下任一选项:

backref('user', order_by=lambda: User.date_modified)
backref('user', order_by='User.date_modified')

可以使用类名
User.date\u modified
访问Class属性,但此时仍未定义类本身。提供可调用(第二种情况在内部转换为可调用)会推迟名称解析,直到首次使用映射,此时所有映射类都已定义。

Hi@AudriusKazukauskas。谢谢你的回答。你能详细解释一下吗?这里的逻辑是什么?这是怎么回事?又来了@audriusKazukauskas。非常感谢你的解释。非常感谢你的帮助!