-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
84 lines (69 loc) · 2.64 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from sqlalchemy import Column, ForeignKey, Integer, String, Table
from sqlalchemy.orm import declarative_base, relationship
from utils import DB_URL
engine = create_engine(DB_URL, echo=True)
Base = declarative_base()
question_form_association = Table(
"question_form_association",
Base.metadata,
Column("question_id", Integer, ForeignKey("questions.id")),
Column("form_id", Integer, ForeignKey("forms.id")),
)
# Define the Form model
class Form(Base):
__tablename__ = "forms"
id = Column(Integer, primary_key=True)
title = Column(String, nullable=False)
questions = relationship(
"Question", secondary=question_form_association, back_populates="forms"
)
responses = relationship("Response", back_populates="form")
# Define the Question model
class Question(Base):
__tablename__ = "questions"
id = Column(Integer, primary_key=True)
text = Column(String, nullable=False)
forms = relationship(
"Form", secondary=question_form_association, back_populates="questions"
)
answers = relationship("Answer", back_populates="question")
# Define the Response model
class Response(Base):
__tablename__ = "responses"
id = Column(Integer, primary_key=True)
form_id = Column(Integer, ForeignKey("forms.id"))
form = relationship("Form", back_populates="responses")
answers = relationship("Answer", back_populates="response")
# Define the Answer model
class Answer(Base):
__tablename__ = "answers"
id = Column(Integer, primary_key=True)
text = Column(String, nullable=False)
question_id = Column(Integer, ForeignKey("questions.id"))
question = relationship("Question", back_populates="answers")
response_id = Column(Integer, ForeignKey("responses.id"))
response = relationship("Response", back_populates="answers")
def create_form_template():
# Create the PostgreSQL database engine
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
# Create a session to interact with the database
session = Session(engine)
# Creating forms
form1 = Form(title="Form 1")
form2 = Form(title="Form 2")
session.add_all([form1, form2])
session.commit()
# Creating questions
question1 = Question(text="What's your name?")
question2 = Question(text="What's your age?")
session.add_all([question1, question2])
session.commit()
# Associating questions with forms
form1.questions.extend([question1, question2])
form2.questions.extend([question1, question2])
session.commit()
session.close()
return f"Tables successfully created!"