-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModels.py
More file actions
33 lines (27 loc) · 986 Bytes
/
Models.py
File metadata and controls
33 lines (27 loc) · 986 Bytes
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
# models.py - database models
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker
# Set up the engine and session
engine = create_engine('sqlite:///./python-simpleapi.db', echo=True)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
class Students(Base):
__tablename__ = 'students'
id = Column(Integer, primary_key=True)
first_name = Column(String)
last_name = Column(String)
email = Column(String)
def __repr__(self):
return f"<User(id={self.id}, first_name='{self.first_name}', last_name='{self.last_name}')>"
def to_dict(self):
return {
"id": self.id,
"first_name": self.first_name,
"last_name": self.last_name,
"email": self.email,
}
@classmethod
def by_id(cls, student_id):
return session.query(cls).filter_by(id=student_id).first()