This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
69 lines (58 loc) · 1.92 KB
/
models.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
from datetime import datetime
from config import db, ma
from marshmallow import fields
class Person(db.Model):
__tablename__ = "person"
person_id = db.Column(db.Integer, primary_key=True)
lname = db.Column(db.String(32))
fname = db.Column(db.String(32))
timestamp = db.Column(
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
)
notes = db.relationship(
"Note",
backref="person",
cascade="all, delete, delete-orphan",
single_parent=True,
order_by="desc(Note.timestamp)",
)
class Note(db.Model):
__tablename__ = "note"
note_id = db.Column(db.Integer, primary_key=True)
person_id = db.Column(db.Integer, db.ForeignKey("person.person_id"))
content = db.Column(db.String(64), nullable=False)
timestamp = db.Column(
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
)
class PersonSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Person
load_instance = True
notes = fields.Nested('PersonNoteSchema', default=[], many=True)
class PersonNoteSchema(ma.SQLAlchemySchema):
"""
This class exists to get around recursion issue
"""
# def __init__(self, **kwargs):
# super().__init__(strict=True, **kwargs)
note_id = fields.Int()
person_id = fields.Int()
content = fields.Str()
timestamp = fields.Str()
class NoteSchema(ma.SQLAlchemyAutoSchema):
# def __init__(self, **kwargs):
# super().__init__(strict=True, **kwargs)
class Meta:
model = Note
load_instance = True
person = fields.Nested("NotePersonSchema", default=None)
class NotePersonSchema(ma.SQLAlchemySchema):
"""
This class exists to get around a recursion issue
"""
# def __init__(self, **kwargs):
# super().__init__(strict=True, **kwargs)
person_id = fields.Int()
lname = fields.Str()
fname = fields.Str()
timestamp = fields.Str()