-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
61 lines (54 loc) · 1.75 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
from datetime import datetime
from config import db, ma
from marshmallow import fields
from sqlalchemy import true
class Person(db.Model):
__tablename__ = 'person'
person_id = db.Column(db.Integer, primary_key=True)
lname = db.Column(db.String(32), index=True)
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, nullable=False)
timestamp = db.Column(
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
)
class PersonSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Person
sqla_session = db.session
load_instance = true
notes = fields.Nested('PersonNoteSchema', default=[], many=True)
class PersonNoteSchema(ma.Schema):
"""
This class exists to get around a recursion issue
"""
note_id = fields.Int()
person_id = fields.Int()
content = fields.Str()
timestamp = fields.Str()
class NoteSchema(ma.Schema):
class Meta:
model = Note
sqla_session = db.session
person = fields.Nested('NotePersonSchema', default=None)
class NotePersonSchema(ma.Schema):
"""
This class exists to get around a recursion issue
"""
person_id = fields.Int()
lname = fields.Str()
fname = fields.Str()
timestamp = fields.Str()