-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_database.py
36 lines (26 loc) · 1.08 KB
/
build_database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship, declarative_base
with open("champions_list.txt", 'r') as f:
champions_list = [line.strip("\n") for line in f.readlines()]
engine = create_engine("sqlite://", echo=True)
Base = declarative_base()
class Quote(Base):
__tablename__ = 'quotes'
id = Column(Integer, primary_key=True, autoincrement=True)
text = Column(String)
answer_id = Column(Integer, ForeignKey('answers.id'))
answer = relationship("Answer", back_populates="quotes")
def __repr__(self):
return f"<Quote(id= {self.id}, text={self.text}, answer={self.answer}>"
class Answer(Base):
__tablename__ = 'answers'
id = Column(Integer, primary_key=True, autoincrement=True)
text = Column(String)
quotes = relationship("Quote", back_populates="answer")
def __repr__(self):
return f"<Answer(text={self.text}, quotes={self.quotes}>"
answer = Answer(text="Answer")
quote = Quote(answer_id=answer.id, text='Quote')
answer.quotes.append(quote)
print(answer)
print(quote)