问题描述
我一直试图了解Python的ORM在填充数据库时的工作方式。所以,让我解释一下我的问题。
我有那些型号:
class Timeline(Base):
__table__ = Table('match_timeline',Base.Metadata,Column("matchId",BigInteger,primary_key=True),Column("platformId",String(7),Column("frameInterval",Integer),Column("framesLength",ForeignKeyConstraint(
["platformId","matchId"],["match.platformId","match.gameId"]
))
frames = relationship('TimelineFrame',backref = 'timeline')
def __repr__(self):
return "Timeline Object for Match %s,platform %s with %s frames" % (str(self.matchId),self.platformId,str(self.framesLength))
def __init__(self,match,timeline_):
self.matchId = match.gameId
self.platformId = match.platformId
self.frameInterval = timeline_['frameInterval']
self.framesLength = len(timeline_['frames'])
class TimelineFrame(Base):
__table__ = Table("match_timeline_frame",Column("match_timeline_matchId",Column("match_timeline_platformId",Column("id",Integer,Column("timestamp",Column("eventsLength",ForeignKeyConstraint(
["match_timeline_matchId","match_timeline_platformId"],["match_timeline.matchId","match_timeline.platformId"]
))
participant_frames = relationship('TimelineParticipantFrame',backref = 'timeline_frame')
participant_events = relationship('TimelineFrameEvent',backref = 'timeline_frame')
def __repr__(self):
return "Match %s,frame %s,timestamp %s,with %s events" % (str(self.match_timeline_matchId),str(self.id),str(self.timestamp),str(self.eventsLength))
def __init__(self,timeline,id_,timeline_):
## id starts from 0
self.match_timeline_matchId = timeline.matchId
self.match_timeline_platformId = timeline.platformId
self.id = id_
self.timestamp = timeline_['frames'][self.id]['timestamp']
self.eventsLength = len(timeline_['frames'][self.id]['events'])
class TimelineParticipantFrame(Base):
__table__ = Table("match_timeline_participant_frame",Column("match_timeline_frame_id",Column("indexId",String),Column("participantId",Column("currentGold",Column("totalGold",Column("level",Column("xp",Column("minionsKilled",Column("jungleMinionsKilled",Column("dominionscore",Column("teamscore",Column("position_x",Column("position_y","match_timeline_platformId","match_timeline_frame_id"],["match_timeline_frame.match_timeline_matchId","match_timeline_frame.match_timeline_platformId","match_timeline_frame.id"]
))
def __repr__(self):
return "Match %s,participant %s,position x = %s and y = %s" % (str(self.match_timeline_matchId),str(self.match_timeline_frame_id),str(self.participantId),str(self.position_x),str(self.position_y))
def __init__(self,timeline_frame,timeline_):
self.match_timeline_matchId = timeline_frame.match_timeline_matchId
self.match_timeline_platformId = timeline_frame.match_timeline_platformId
self.match_timeline_frame_id = timeline_frame.id
self.indexId = str(id_)
pid_obj = timeline_['frames'][self.match_timeline_frame_id]['participantFrames'][self.indexId]
self.participantId = pid_obj['participantId']
self.position_x = pid_obj['position']['x']
self.position_y = pid_obj['position']['y']
resting_values = {}
for key,value in pid_obj.items():
if key != 'position':
resting_values[key] = value
super().__init__(**resting_values)
print(self.minionsKilled)
class TimelineFrameEvent(Base):
__table__ = Table("match_timeline_frame_event",Column("eventId",Column("type",String,nullable = False),Column("itemId",Column("skillSlot",Column("creatorId",Column("teamId",Column("killerId",Column("levelUpType",Column("wardType",# Column("pointCaptured",String(20)),Column("victimId",Column("afterId",Column("beforeId",Column("assistingParticipantIds",PickleType),Column("monsterType",Column("monsterSubType",Column("buildingType",Column("laneType",Column("towerType","match_timeline_frame.id"]
))
def __repr__(self):
return "Match %s,event %s,timestamp %s" % (str(self.match_timeline_matchId),str(self.type),str(self.timestamp))
def __init__(self,timeline_):
# eventId goes from 0 to the number of events in that minute
self.match_timeline_matchId = timeline_frame.match_timeline_matchId
self.match_timeline_platformId = timeline_frame.match_timeline_platformId
self.match_timeline_frame_id = timeline_frame.id
self.eventId = id_
event_obj = timeline_['frames'][self.match_timeline_frame_id]['events'][self.eventId]
self.type = event_obj['type']
self.timestamp = event_obj['timestamp']
resting_values = {}
for key,value in event_obj.items():
if key == 'position':
resting_values['position_x'] = event_obj['position']['x']
resting_values['position_x'] = event_obj['position']['x']
elif key == 'type' or key == 'timestamp':
pass
else:
resting_values[key] = value
super().__init__(**resting_values)
然后,我通过一个具有多个深度的json来填充数据库,并且我正在从json创建一种解析器,实例化并从架构/模型创建与这些json相关的所有对象。我已经定义了。
总体来说,一个时间轴对象代表多个timeline_frame(一对多关系),timeline_frame代表多个参与者帧(一对多关系)和多个frame_events(也就是一对多)。
我能够实例化所有这些对象,但是,我不确定如何将这些实例化的对象上载到数据库中。
Timeline,then link it to the TimelineFrames,then for each TimelineFrame creates all the ParticipantFrames and FramesEvents and link them to their parent TimelineFrames
i.e. :
def create_timelines(mi,tl):
## mi and tl are the json results of the request of the API I want to store the data from
## Supposed to creates all the objects before pushing them to the database
## Match is another object which I need to instantiate timeline
match = Match(mi)
## Timeline objects creations:
timeline = Timeline(match,tl)
for i in range(0,timeline.framesLength): ## For each minutes
timeline_frame = TimelineFrame(tl,i,timeline)
for j in range(1,11):## I instantiate and append to the timeline_frame objects the children objects
timeline_participant_frame = TimelineParticipantFrame(timeline_frame,str(j),tl)
timeline_frames.participant_append(timeline_participant_frame)
for k in range(0,timeline_frame.eventsLength): ## I instantiate and append to the timeline_frame objects the children objects
timeline_frame_event = TimelineFrameEvent(timeline_frame,k,tl)
timeline_frame.participant_events.append(timeline_frame_event)
## I instantiate and append to the timeline object the timeline_frame fully equipped of the children object
timeline.frames.append(timeline_frame)
但是问题是,我何时应该session.add(object)这些对象,以及我应该添加哪些对象?由于所有对象相互链接,我应该添加父时间轴对象,该对象会自动添加所有子对象吗?
如果可以的话,感谢您的阅读和帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)