-
Notifications
You must be signed in to change notification settings - Fork 27
/
orm-5-relationships.py
98 lines (74 loc) · 2.68 KB
/
orm-5-relationships.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
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
# Base
Base = declarative_base()
# Concrete type
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True)
name = Column(String)
def __repr__(self):
return "<User(id: %r, name: %r)>" % (self.id, self.name)
# Engine and create tables
from sqlalchemy import create_engine
engine = create_engine("sqlite://", echo=True)
Base.metadata.create_all(engine)
# Session with identity map
from sqlalchemy.orm import Session
session = Session(bind=engine)
# adding multiple objects as *pending*
u1 = User(name="slavo")
session.add_all([
u1,
User(name="jano"),
User(name="vlado"),
User(name="peter"),
User(name="brano")
])
# finalize transaction
session.commit();
#-------------------------------------------------------------------------------
# Many-to-One relationship (Adr->User - one user can live on multiple addresses)
#-------------------------------------------------------------------------------
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
# in sqlalchemy we have to declare relation ship twice
# 1) relation type at core level
# 2) relationship on orm level and object level
class Address(Base):
__tablename__ = "address"
id = Column(Integer, primary_key=True)
email = Column(String, nullable=False)
user_id = Column(Integer, ForeignKey("user.id"))
user = relationship("User", backref="addresses") # creates addresses property on referenced object
def __repr__(self):
return "<Address(%r)>" % self.email
# Creates addresses table
Base.metadata.create_all(engine)
u1 = User(name="Matus")
print(u1) # <User(id: None, name: 'Matus')>
print(u1.addresses) # []
u1.addresses = [
Address(email="[email protected]"),
Address(email="[email protected]"),
Address(email="[email protected]")
]
# everything is in-memory now
print(u1)
print(u1.addresses[1])
print(u1.addresses[1].user)
# Cascading objects when adding into session
session.add(u1) # also added addresses
print(session.new)
# write changes to db, ends transaction, data are expired
session.commit()
# after expiration, user.addresses emits *lazy loading* when first accessed
print(u1)
print(u1.addresses) # now collection is in-memory until transaction ends
print(u1.addresses) # won't hit database as data are already in memory
# collections and references are updated by manipulating objects, not primary/foreign keys
v = session.query(User).filter(User.name == "vlado").one();
u1.addresses[1].user = v; # user vlado now has one address, user u1 has two
session.commit()
print(u1.addresses)
print(v.addresses)