我有一個多對一的關係數據庫。在我的聲明系統中,我打電話給relationship(),如下例所示。出於某種原因,我想保留所有Features
排序。不幸的是,排序邏輯非常複雜,並且使用多個屬性來完成。如何在SQLAlchemy關係()中使用sortedcontainers.SortedList?
from sortedcontainers import SortedList
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, Text
from sqlalchemy import ForeignKey
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
class Product(Base):
__tablename__ = 'products'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(Text)
class Feature(Base):
__tablename__ = 'features'
id = Column(Integer, primary_key=True, autoincrement=True)
productid = Column(Integer, ForeignKey('products.id'))
attr = Column(Integer)
product = relationship('Product', back_populates='features')
Product.features=relationship('Feature', collection_class=SortedList, back_populates='product')
def __eq__(self, other):
# some magic here, simplified...
return self.attr == other.attr
def __lt__(self, other):
# some magic here, simplified...
return self.attr < other.attr
def __le__(self, other):
# some magic here, simplified...
return self.attr <= other.attr
engine = create_engine('sqlite:///test.db')
Session = sessionmaker(bind=engine)
Base.metadata.create_all(engine)
我已經充滿了下列值的數據庫:
sqlite> select * from products;
id name
---------- ----------
1 Product1
sqlite> select * from features;
id productid attr
---------- ---------- ----------
1 1 42
2 1 1
當我嘗試加載(通過訪問)的值,sortedcontainers.SortedList引發一個ValueError異常,大概是因爲它正在嘗試加載無序結果。當功能按順序存儲時,沒有問題。
In [1]: from test import *
In [2]: session = Session()
In [3]: p = session.query(Product).first()
In [4]: for f in p.features:
print("id: %d\tattr: %d" % (f.id, f.attr))
...:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
# traceback
ValueError: <test.Feature object at 0x7f68da5d2208> not in sort order at index 1
我知道在這個特殊的情況下,解決方案是使用relationship
()的說法order_by
但我的使用情況是更復雜,因爲排序使用幾個列的值。
那麼,有沒有我錯過了使用SQLAlchemy或我應該嘗試找到一種解決方法?