forked from graphql-python/graphene-sqlalchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
278 lines (205 loc) · 7.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from __future__ import absolute_import
import datetime
import enum
from decimal import Decimal
from typing import List, Optional, Tuple
from sqlalchemy import (
Column,
Date,
Enum,
ForeignKey,
Integer,
Numeric,
String,
Table,
func,
select,
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import column_property, composite, 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 CompositeFullName(object):
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
def __composite_values__(self):
return self.first_name, self.last_name
def __repr__(self):
return "{} {}".format(self.first_name, self.last_name)
class Reporter(Base):
__tablename__ = "reporters"
id = Column(Integer(), primary_key=True)
first_name = Column(String(30), doc="First name")
last_name = Column(String(30), doc="Last name")
email = Column(String(), doc="Email")
favorite_pet_kind = Column(PetKind)
pets = relationship(
"Pet",
secondary=association_table,
backref="reporters",
order_by="Pet.id",
lazy="selectin",
)
articles = relationship("Article", backref="reporter", lazy="selectin")
favorite_article = relationship("Article", uselist=False, lazy="selectin")
@hybrid_property
def hybrid_prop_with_doc(self):
"""Docstring test"""
return self.first_name
@hybrid_property
def hybrid_prop(self):
return self.first_name
@hybrid_property
def hybrid_prop_str(self) -> str:
return self.first_name
@hybrid_property
def hybrid_prop_int(self) -> int:
return 42
@hybrid_property
def hybrid_prop_float(self) -> float:
return 42.3
@hybrid_property
def hybrid_prop_bool(self) -> bool:
return True
@hybrid_property
def hybrid_prop_list(self) -> List[int]:
return [1, 2, 3]
column_prop = column_property(
select([func.cast(func.count(id), Integer)]), doc="Column property"
)
composite_prop = composite(
CompositeFullName, first_name, last_name, doc="Composite"
)
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"))
readers = relationship(
"Reader", secondary="articles_readers", back_populates="articles"
)
class Reader(Base):
__tablename__ = "readers"
id = Column(Integer(), primary_key=True)
name = Column(String(100))
articles = relationship(
"Article", secondary="articles_readers", back_populates="readers"
)
class ArticleReader(Base):
__tablename__ = "articles_readers"
article_id = Column(Integer(), ForeignKey("articles.id"), primary_key=True)
reader_id = Column(Integer(), ForeignKey("readers.id"), primary_key=True)
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)
############################################
# The models below are mainly used in the
# @hybrid_property type inference scenarios
############################################
class ShoppingCartItem(Base):
__tablename__ = "shopping_cart_items"
id = Column(Integer(), primary_key=True)
@hybrid_property
def hybrid_prop_shopping_cart(self) -> List["ShoppingCart"]:
return [ShoppingCart(id=1)]
class ShoppingCart(Base):
__tablename__ = "shopping_carts"
id = Column(Integer(), primary_key=True)
# Standard Library types
@hybrid_property
def hybrid_prop_str(self) -> str:
return self.first_name
@hybrid_property
def hybrid_prop_int(self) -> int:
return 42
@hybrid_property
def hybrid_prop_float(self) -> float:
return 42.3
@hybrid_property
def hybrid_prop_bool(self) -> bool:
return True
@hybrid_property
def hybrid_prop_decimal(self) -> Decimal:
return Decimal("3.14")
@hybrid_property
def hybrid_prop_date(self) -> datetime.date:
return datetime.datetime.now().date()
@hybrid_property
def hybrid_prop_time(self) -> datetime.time:
return datetime.datetime.now().time()
@hybrid_property
def hybrid_prop_datetime(self) -> datetime.datetime:
return datetime.datetime.now()
# Lists and Nested Lists
@hybrid_property
def hybrid_prop_list_int(self) -> List[int]:
return [1, 2, 3]
@hybrid_property
def hybrid_prop_list_date(self) -> List[datetime.date]:
return [self.hybrid_prop_date, self.hybrid_prop_date, self.hybrid_prop_date]
@hybrid_property
def hybrid_prop_nested_list_int(self) -> List[List[int]]:
return [
self.hybrid_prop_list_int,
]
@hybrid_property
def hybrid_prop_deeply_nested_list_int(self) -> List[List[List[int]]]:
return [
[
self.hybrid_prop_list_int,
],
]
# Other SQLAlchemy Instances
@hybrid_property
def hybrid_prop_first_shopping_cart_item(self) -> ShoppingCartItem:
return ShoppingCartItem(id=1)
# Other SQLAlchemy Instances
@hybrid_property
def hybrid_prop_shopping_cart_item_list(self) -> List[ShoppingCartItem]:
return [ShoppingCartItem(id=1), ShoppingCartItem(id=2)]
# Unsupported Type
@hybrid_property
def hybrid_prop_unsupported_type_tuple(self) -> Tuple[str, str]:
return "this will actually", "be a string"
# Self-references
@hybrid_property
def hybrid_prop_self_referential(self) -> "ShoppingCart":
return ShoppingCart(id=1)
@hybrid_property
def hybrid_prop_self_referential_list(self) -> List["ShoppingCart"]:
return [ShoppingCart(id=1)]
# Optional[T]
@hybrid_property
def hybrid_prop_optional_self_referential(self) -> Optional["ShoppingCart"]:
return None
class KeyedModel(Base):
__tablename__ = "test330"
id = Column(Integer(), primary_key=True)
reporter_number = Column("% reporter_number", Numeric, key="reporter_number")