-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_polymorphic_self_reference.py
68 lines (55 loc) · 1.86 KB
/
test_polymorphic_self_reference.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
from sqlalchemy import Column, Integer, Boolean
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship, backref
from sqlalchemy.schema import Table
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from base import ModelBase, get_sql_table_data
customer_organization = Table(
'base_user_customer_organization', ModelBase.metadata,
Column('user_id', Integer, ForeignKey('base_user.id')),
Column('org_id', Integer, ForeignKey('base_user.id'))
)
class BaseUser(ModelBase):
__tablename__ = 'base_user'
id = Column(Integer, primary_key=True, nullable=False)
org = Column(Boolean, default=False, nullable=False)
# Shared Fields
__mapper_args__ = {
'polymorphic_on': org,
}
customers = relationship(
"BaseUser",
backref=backref('organization', order_by=id),
secondary=customer_organization,
primaryjoin=id == customer_organization.c.org_id and org is True,
secondaryjoin=id == customer_organization.c.user_id and org is False
)
class CustomerUser(BaseUser):
# Customer Fields
__mapper_args__ = {
'polymorphic_identity': False
}
class OrganizationUser(BaseUser):
# Organization Fields
__mapper_args__ = {
'polymorphic_identity': True
}
if __name__ == "__main__":
engine = create_engine("sqlite://")
ModelBase.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
sql = Session()
#sql = sqldb.get_session()
customer1 = CustomerUser()
sql.add(customer1)
customer2 = CustomerUser()
sql.add(customer2)
organization = OrganizationUser()
organization.customers = [customer1, customer2]
sql.add(organization)
sql.commit()
print get_sql_table_data(sql, BaseUser)
print organization.customers
print customer1.organization
print customer2.organization