8

在我正在处理的 Pyramid 应用程序中,我有以下场景:

class Widget(Base):
    __tablename__ = 'widgets'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    sidebar = Column(mysql.TINYINT(2))

    def __init__(self, name, sidebar):
        self.name = name
        self.sidebar = sidebar

class Dashboard(Base):
    __tablename__ = 'dashboard'
    user_id = Column(Integer, ForeignKey('users.id'), primary_key=True)
    widget_id = Column(Integer, ForeignKey('widgets.id'), primary_key=True)
    delta = Column(mysql.TINYINT)

    widget = relationship('Widget')

    def __init__(self, user_id, widget_id, delta):
        self.user_id = user_id
        self.widget_id = widget_id
        self.delta = delta 

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    login = Column(Unicode(255), unique=True)
    password = Column(Unicode(60))
    fullname = Column(Unicode(100))

    dashboard = relationship('Dashboard', order_by='Dashboard.widget.sidebar, Dashboard.delta')

    def __init__(self, login, password, fullname):
        self.login = login
        self.password = crypt.encode(password)
        self.fullname = fullname

因此,我希望用户“仪表板”关系具有用户的仪表板记录,但按“侧边栏”(这是仪表板的关系属性)排序。目前我收到此错误:

sqlalchemy.exc.InvalidRequestError: Property 'widget' is not an instance of ColumnProperty (i.e. does not correspond directly to a Column).

在关系声明中可以进行这种排序吗?

谢谢!

4

1 回答 1

6

有了这个,试着想想 SQL SQLAlchemy 在尝试加载 User.dashboard 时应该发出什么。喜欢SELECT * FROM dashboard JOIN widget ... ORDER BY widget.sidebar ?或者SELECT * FROM dashboard ORDER BY (SELECT sidebar FROM widget...?通过不同的表格对结果进行排序对于一项工作来说过于开放,relationship()无法自行决定。可以做到这一点的方法是通过提供一个列表达式Dashboard来提供这种排序,当 ORM 针对仪表板的表发出一个简单的 SELECT 时,以及当它在一个不那么简单的 SELECT 中引用它时可能会同时跨用户、仪表板表加入(例如急切加载)。

我们提供自定义 SQL 表达式,特别是那些涉及其他表的表达式,使用column_property(),或者当我们不希望默认加载该表达式时使用deferred() (很可能是这里的情况)。例子:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()

class Widget(Base):
    __tablename__ = 'widgets'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    sidebar = Column(Integer)

class Dashboard(Base):
    __tablename__ = 'dashboard'
    user_id = Column(Integer, ForeignKey('users.id'), primary_key=True)
    widget_id = Column(Integer, ForeignKey('widgets.id'), primary_key=True)
    delta = Column(Integer)

    widget = relationship('Widget')

    widget_sidebar = deferred(select([Widget.sidebar]).where(Widget.id == widget_id))

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    login = Column(Unicode(255), unique=True)

    dashboard = relationship('Dashboard', order_by='Dashboard.widget_sidebar, Dashboard.delta')


e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

s = Session(e)

w1, w2 = Widget(name='w1', sidebar=1), Widget(name='w2', sidebar=2)
s.add_all([
    User(login='u1', dashboard=[
        Dashboard(
            delta=1, widget=w1
        ),
        Dashboard(
            delta=2, widget=w2
        )
    ]),
])
s.commit()

print s.query(User).first().dashboard

加载“.dashboard”发出的最终 SQL 是:

SELECT dashboard.user_id AS dashboard_user_id, dashboard.widget_id AS dashboard_widget_id, dashboard.delta AS dashboard_delta 
FROM dashboard 
WHERE ? = dashboard.user_id ORDER BY (SELECT widgets.sidebar 
FROM widgets 
WHERE widgets.id = dashboard.widget_id), dashboard.delta

请记住,MySQL 在优化上述子查询方面做得很糟糕。如果您在这里需要高性能,您可能会考虑将“sidebar”的值复制到“dashboard”中,即使这会使一致性更加难以维护。

于 2013-11-02T21:49:53.473 回答