-
Notifications
You must be signed in to change notification settings - Fork 5
/
constraint_mixin.py
70 lines (53 loc) · 2.01 KB
/
constraint_mixin.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
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from base import ModelBase, get_sql_table_data
# http://stackoverflow.com/questions/34945722/flask-sqlalchemy-many-to-many-adjacency-list-changes-after-commit/34950945?noredirect=1
import sqlalchemy as db
from sqlalchemy.ext.declarative import declared_attr
db.Model = ModelBase
class FilteringMixin(object):
FILTER_TYPES = {
'whitelist': 0,
'blacklist': 1,
}
@declared_attr
def filter_type(cls):
return db.Column(db.SmallInteger, nullable=False,
default=cls.FILTER_TYPES['whitelist'])
@declared_attr
def __table_args__(cls):
return (
db.CheckConstraint(
'filter_type in (%s)' %
','.join(str(t) for t in cls.FILTER_TYPES.values())),
)
class FilteredConnectionType(FilteringMixin, db.Model):
CONNECTION_TYPES = {
'wifi': 0,
'cellular': 1,
}
__tablename__ = 'filtered_connection_types'
# Changed to db.Integer from db.BigInteger because of SQLite
id = db.Column(db.Integer, primary_key=True)
connection_type = db.Column(db.SmallInteger, nullable=False,
default=CONNECTION_TYPES['cellular'])
@declared_attr
def __table_args__(cls):
return (
db.CheckConstraint(cls.connection_type.in_(cls.CONNECTION_TYPES.values())),
) + FilteringMixin.__table_args__
if __name__ == "__main__":
engine = create_engine("sqlite://")
ModelBase.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
# saves the record with defaults
conn_type = FilteredConnectionType()
session.add(conn_type)
session.commit()
print get_sql_table_data(session, FilteredConnectionType)
# raises an error: Check constraint failed
conn_type = FilteredConnectionType(filter_type=3)
session.add(conn_type)
session.commit()
print get_sql_table_data(session, FilteredConnectionType)