所以我对 SQLAlchemy 很陌生。
我有一个模型 Showing,它在表中有大约 10,000 行。这是课程:
class Showing(Base):
__tablename__ = "showings"
id = Column(Integer, primary_key=True)
time = Column(DateTime)
link = Column(String)
film_id = Column(Integer, ForeignKey('films.id'))
cinema_id = Column(Integer, ForeignKey('cinemas.id'))
def __eq__(self, other):
if self.time == other.time and self.cinema == other.cinema and self.film == other.film:
return True
else:
return False
如果新节目尚不存在,谁能给我一些关于插入新节目的最快方法的指导。我认为它稍微复杂一些,因为只有在时间、电影和电影在放映中是独一无二的情况下,放映才是独一无二的。
我目前有这个代码:
def AddShowings(self, showing_times, cinema, film):
all_showings = self.session.query(Showing).options(joinedload(Showing.cinema), joinedload(Showing.film)).all()
for showing_time in showing_times:
tmp_showing = Showing(time=showing_time[0], film=film, cinema=cinema, link=showing_time[1])
if tmp_showing not in all_showings:
self.session.add(tmp_showing)
self.session.commit()
all_showings.append(tmp_showing)
这有效,但似乎很慢。任何帮助深表感谢。