【发布时间】:2018-03-04 12:05:10
【问题描述】:
Python 3.6 和 SQLAlchemy 1.2。
我有一个名为events 的包,它将Match 定义为Event 的一种类型,并使用连接表继承将其与其他类型的Event 区分开来。另一种类型的事件是Competition,所有事件都是其中之一:
class Event(Base):
__tablename__ = 'tbl_events'
event_id = Column(Integer, primary_key=True)
event_type_id = Column(String(50)) # 0 is Match, 1 is Competition
__mapper_args__ = {'polymorphic_on': event_type_id}
class Match(Event):
__tablename__ = 'tbl_match_details'
event_id = Column(Integer,
ForeignKey('tbl_events.event_id'),
primary_key=True)
team_1 = Column(String(50))
team_2 = Column(String(50))
__mapper_args__ = {'polymorphic_identity': 0}
我在另一个包中使用Match,它区分多种类型的Match,并依赖Match对象的属性和方法从数据库中提取事件信息,但在远离数据库的情况下操作:
from events import Match
class BaseMatch(Match):
# define common methods and attrs
class TennisMatch(BaseMatch):
# do Tennis based stuff
class FootballMatch(BaseMatch):
# do football based things
events.Match 和从它继承的类之间的任何区别仅在此包中很重要,并且此包不会插入或更新数据库,只会从中读取。
我遇到的问题是,尝试实例化从Match 继承的任何类的实例会导致NULL 值被传递到event_type_id 字段的查询中。这是查询的WHERE 部分:
WHERE tbl_match_details.event_id = %s AND tbl_events.match_comp_id IN (NULL)
我不能简单地给每个类自己的多态标识符,因为这些标识符不会存在于数据库中。
我试过这个:
class BaseMatch(Match):
@declared_attr
def __mapper_args__(cls):
return {'polymorphic_identity': 0}
class TennisMatch(BaseMatch):
# do tennis stuff
class FootballMatch(BaseMatch):
# do footy stuff
但导入模块时,我收到如下警告:
SAWarning: Reassigning polymorphic association for identity 0 from <Mapper at 0x7f80197f0550; Match> to <Mapper at 0x7f80197a9fd0; BaseModel>: Check for duplicate use of 0 as value for polymorphic_identity.
SAWarning: Reassigning polymorphic association for identity 0 from <Mapper at 0x7f80197a9fd0; BaseModel> to <Mapper at 0x7f800dfdf940; TennisMatch>: Check for duplicate use of 0 as value for polymorphic_identity.
我为从Match 继承的每个类获取其中一个,当我尝试实例化任何匹配类型时,我得到最后一个与该多态 id 关联的类型的实例。
我真的很感激朝着正确的方向轻推!
谢谢。
【问题讨论】:
标签: python python-3.x orm sqlalchemy