forked from sqlalchemy/sqlalchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle.py
173 lines (137 loc) · 4.29 KB
/
single.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
"""Single-table (table-per-hierarchy) inheritance example."""
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import ForeignKey
from sqlalchemy import inspect
from sqlalchemy import Integer
from sqlalchemy import or_
from sqlalchemy import String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import relationship
from sqlalchemy.orm import Session
from sqlalchemy.orm import with_polymorphic
Base = declarative_base()
class Company(Base):
__tablename__ = "company"
id = Column(Integer, primary_key=True)
name = Column(String(50))
employees = relationship(
"Person", back_populates="company", cascade="all, delete-orphan"
)
def __repr__(self):
return "Company %s" % self.name
class Person(Base):
__tablename__ = "person"
id = Column(Integer, primary_key=True)
company_id = Column(ForeignKey("company.id"))
name = Column(String(50))
type = Column(String(50))
company = relationship("Company", back_populates="employees")
__mapper_args__ = {
"polymorphic_identity": "person",
"polymorphic_on": type,
}
def __repr__(self):
return "Ordinary person %s" % self.name
class Engineer(Person):
engineer_name = Column(String(30))
primary_language = Column(String(30))
# illustrate a single-inh "conflicting" column declaration;
# see http://docs.sqlalchemy.org/en/latest/orm/extensions/
# declarative/inheritance.html#resolving-column-conflicts
@declared_attr
def status(cls):
return Person.__table__.c.get("status", Column(String(30)))
__mapper_args__ = {"polymorphic_identity": "engineer"}
def __repr__(self):
return (
"Engineer %s, status %s, engineer_name %s, "
"primary_language %s"
% (
self.name,
self.status,
self.engineer_name,
self.primary_language,
)
)
class Manager(Person):
manager_name = Column(String(30))
@declared_attr
def status(cls):
return Person.__table__.c.get("status", Column(String(30)))
__mapper_args__ = {"polymorphic_identity": "manager"}
def __repr__(self):
return "Manager %s, status %s, manager_name %s" % (
self.name,
self.status,
self.manager_name,
)
engine = create_engine("sqlite://", echo=True)
Base.metadata.create_all(engine)
session = Session(engine)
c = Company(
name="company1",
employees=[
Manager(
name="pointy haired boss", status="AAB", manager_name="manager1"
),
Engineer(
name="dilbert",
status="BBA",
engineer_name="engineer1",
primary_language="java",
),
Person(name="joesmith"),
Engineer(
name="wally",
status="CGG",
engineer_name="engineer2",
primary_language="python",
),
Manager(name="jsmith", status="ABA", manager_name="manager2"),
],
)
session.add(c)
session.commit()
c = session.query(Company).get(1)
for e in c.employees:
print(e, inspect(e).key, e.company)
assert set([e.name for e in c.employees]) == set(
["pointy haired boss", "dilbert", "joesmith", "wally", "jsmith"]
)
print("\n")
dilbert = session.query(Person).filter_by(name="dilbert").one()
dilbert2 = session.query(Engineer).filter_by(name="dilbert").one()
assert dilbert is dilbert2
dilbert.engineer_name = "hes dilbert!"
session.commit()
c = session.query(Company).get(1)
for e in c.employees:
print(e)
# query using with_polymorphic.
eng_manager = with_polymorphic(Person, [Engineer, Manager])
print(
session.query(eng_manager)
.filter(
or_(
eng_manager.Engineer.engineer_name == "engineer1",
eng_manager.Manager.manager_name == "manager2",
)
)
.all()
)
# illustrate join from Company,
eng_manager = with_polymorphic(Person, [Engineer, Manager])
print(
session.query(Company)
.join(Company.employees.of_type(eng_manager))
.filter(
or_(
eng_manager.Engineer.engineer_name == "engineer1",
eng_manager.Manager.manager_name == "manager2",
)
)
.all()
)
session.commit()