Commit a79bce9944443aa228d998190c55a421d06ba94c

Authored by Miguel Barão
1 parent 6181d078
Exists in dev

update models.py new sqlalchemy 2.0 style

Showing 1 changed file with 39 additions and 36 deletions   Show diff stats
aprendizations/models.py
1 1
  2 +# FIXME Mapped[...] probably wrong for the relationships.
  3 +# See sqlalchemy documentation:
  4 +# https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html
  5 +
  6 +from typing import List
  7 +
2 # third party libraries 8 # third party libraries
3 -from sqlalchemy import Column, ForeignKey, Integer, Float, String  
4 -from sqlalchemy.orm import declarative_base, relationship 9 +from sqlalchemy import ForeignKey, Integer, Float, String
  10 +# from sqlalchemy.orm import declarative_base, relationship
  11 +from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
5 12
6 13
7 # =========================================================================== 14 # ===========================================================================
8 -# Declare ORM  
9 -# FIXME Any is a workaround for mypy static type checking (see https://github.com/python/mypy/issues/6372)  
10 -# from typing import Any  
11 -# Base: Any = declarative_base()  
12 -Base = declarative_base()  
13 - 15 +class Base(DeclarativeBase):
  16 + pass
14 17
15 # --------------------------------------------------------------------------- 18 # ---------------------------------------------------------------------------
16 class StudentTopic(Base): 19 class StudentTopic(Base):
17 __tablename__ = 'studenttopic' 20 __tablename__ = 'studenttopic'
18 - student_id = Column(String, ForeignKey('students.id'), primary_key=True)  
19 - topic_id = Column(String, ForeignKey('topics.id'), primary_key=True)  
20 - level = Column(Float)  
21 - date = Column(String) 21 + student_id: Mapped[str] = mapped_column(ForeignKey('students.id'), primary_key=True)
  22 + topic_id: Mapped[str] = mapped_column(ForeignKey('topics.id'), primary_key=True)
  23 + level: Mapped[float]
  24 + date: Mapped[str]
22 25
23 - # ---  
24 - student = relationship('Student', back_populates='topics')  
25 - topic = relationship('Topic', back_populates='students') 26 + # --- FIXME
  27 + student: Mapped["Student"] = relationship('Student', back_populates='topics')
  28 + topic: Mapped["Topic"] = relationship('Topic', back_populates='students')
26 29
27 def __repr__(self): 30 def __repr__(self):
28 return ('StudentTopic(' 31 return ('StudentTopic('
@@ -37,13 +40,13 @@ class StudentTopic(Base): @@ -37,13 +40,13 @@ class StudentTopic(Base):
37 # --------------------------------------------------------------------------- 40 # ---------------------------------------------------------------------------
38 class Student(Base): 41 class Student(Base):
39 __tablename__ = 'students' 42 __tablename__ = 'students'
40 - id = Column(String, primary_key=True)  
41 - name = Column(String)  
42 - password = Column(String) 43 + id: Mapped[str] = mapped_column(String, primary_key=True)
  44 + name: Mapped[str]
  45 + password: Mapped[str]
43 46
44 - # ---  
45 - answers = relationship('Answer', back_populates='student')  
46 - topics = relationship('StudentTopic', back_populates='student') 47 + # --- FIXME
  48 + answers: Mapped[List["Answer"]] = relationship('Answer', back_populates='student')
  49 + topics: Mapped[List["StudentTopic"]] = relationship('StudentTopic', back_populates='student')
47 50
48 def __repr__(self): 51 def __repr__(self):
49 return ('Student(' 52 return ('Student('
@@ -57,17 +60,17 @@ class Student(Base): @@ -57,17 +60,17 @@ class Student(Base):
57 # --------------------------------------------------------------------------- 60 # ---------------------------------------------------------------------------
58 class Answer(Base): 61 class Answer(Base):
59 __tablename__ = 'answers' 62 __tablename__ = 'answers'
60 - id = Column(Integer, primary_key=True) # auto_increment  
61 - ref = Column(String)  
62 - grade = Column(Float)  
63 - starttime = Column(String)  
64 - finishtime = Column(String)  
65 - student_id = Column(String, ForeignKey('students.id'))  
66 - topic_id = Column(String, ForeignKey('topics.id'))  
67 -  
68 - # ---  
69 - student = relationship('Student', back_populates='answers')  
70 - topic = relationship('Topic', back_populates='answers') 63 + id: Mapped[int] = mapped_column(primary_key=True) # auto_increment
  64 + ref: Mapped[str]
  65 + grade: Mapped[float]
  66 + starttime: Mapped[str]
  67 + finishtime: Mapped[str]
  68 + student_id: Mapped[str] = mapped_column(String, ForeignKey('students.id'))
  69 + topic_id: Mapped[str] = mapped_column(String, ForeignKey('topics.id'))
  70 +
  71 + # --- FIXME
  72 + student: Mapped["Student"] = relationship('Student', back_populates='answers')
  73 + topic: Mapped["Topic"] = relationship('Topic', back_populates='answers')
71 74
72 def __repr__(self): 75 def __repr__(self):
73 return ('Question(' 76 return ('Question('
@@ -85,11 +88,11 @@ class Answer(Base): @@ -85,11 +88,11 @@ class Answer(Base):
85 # --------------------------------------------------------------------------- 88 # ---------------------------------------------------------------------------
86 class Topic(Base): 89 class Topic(Base):
87 __tablename__ = 'topics' 90 __tablename__ = 'topics'
88 - id = Column(String, primary_key=True) 91 + id: Mapped[str] = mapped_column(primary_key=True)
89 92
90 - # ---  
91 - students = relationship('StudentTopic', back_populates='topic')  
92 - answers = relationship('Answer', back_populates='topic') 93 + # --- FIXME
  94 + students: Mapped["StudentTopic"] = relationship('StudentTopic', back_populates='topic')
  95 + answers: Mapped["Answer"] = relationship('Answer', back_populates='topic')
93 96
94 def __repr__(self): 97 def __repr__(self):
95 return f'Topic(id={self.id!r})' 98 return f'Topic(id={self.id!r})'