forked from graphql-python/graphene-sqlalchemy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
78 lines (55 loc) · 2.01 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
70
71
72
73
74
75
76
77
78
from __future__ import absolute_import
import enum
from sqlalchemy import Column, Date, Enum, ForeignKey, Integer, String, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import mapper, relationship
PetKind = Enum("cat", "dog", name="pet_kind")
class HairKind(enum.Enum):
LONG = 'long'
SHORT = 'short'
Base = declarative_base()
association_table = Table(
"association",
Base.metadata,
Column("pet_id", Integer, ForeignKey("pets.id")),
Column("reporter_id", Integer, ForeignKey("reporters.id")),
)
class Editor(Base):
__tablename__ = "editors"
editor_id = Column(Integer(), primary_key=True)
name = Column(String(100))
class Pet(Base):
__tablename__ = "pets"
id = Column(Integer(), primary_key=True)
name = Column(String(30))
pet_kind = Column(PetKind, nullable=False)
hair_kind = Column(Enum(HairKind, name="hair_kind"), nullable=False)
reporter_id = Column(Integer(), ForeignKey("reporters.id"))
class Reporter(Base):
__tablename__ = "reporters"
id = Column(Integer(), primary_key=True)
first_name = Column(String(30))
last_name = Column(String(30))
email = Column(String())
favorite_pet_kind = Column(PetKind)
pets = relationship("Pet", secondary=association_table, backref="reporters")
articles = relationship("Article", backref="reporter")
favorite_article = relationship("Article", uselist=False)
# total = column_property(
# select([
# func.cast(func.count(PersonInfo.id), Float)
# ])
# )
class Article(Base):
__tablename__ = "articles"
id = Column(Integer(), primary_key=True)
headline = Column(String(100))
pub_date = Column(Date())
reporter_id = Column(Integer(), ForeignKey("reporters.id"))
class ReflectedEditor(type):
"""Same as Editor, but using reflected table."""
@classmethod
def __subclasses__(cls):
return []
editor_table = Table("editors", Base.metadata, autoload=True)
mapper(ReflectedEditor, editor_table)