有一些型号:
from sqlalchemy import create_engine, Table, Column, Integer, \
String, MetaData, join, ForeignKey, select
from sqlalchemy.orm import sessionmaker, column_property, relationship
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///memory')
s = sessionmaker(bind = engine)()
Base = declarative_base()
metadata = MetaData()
class BaseItem(Base):
__tablename__ = 'base_item'
id = Column(Integer, primary_key=True)
name = Column(Integer)
class Stats(Base):
__tablename__ = 'stats'
id = Column(Integer, primary_key=True)
base_item_id = Column(ForeignKey('base_item.id'))
key = Column(String(60))
value = Column(Integer)
class ItemProxy(Base):
__tablename__ = 'item_proxy'
id = Column(Integer, primary_key=True)
base_item_id = Column(ForeignKey('base_item.id'))
some = Column(Integer)
bonus_item_id = Column(ForeignKey('base_item.id'))
class Item(Base):
__table__ = BaseItem.__table__.join(ItemProxy.__table__, ItemProxy.__table__.c.base_item_id == BaseItem.__table__.c.id)
id = ItemProxy.__table__.c.id
base_id = column_property(BaseItem.__table__.c.id, ItemProxy.__table__.c.base_item_id)
stat = relationship('Stats')
bonus_stat = relationship('Stats', ???)
模型ItemProxy bonus_item_id字段不会一直填充。 您需要指定一个能使其成为正确查询的关系吗?