1
+ from sqlalchemy import Column , ForeignKey , Integer , String
2
+ from sqlalchemy .ext .declarative import declarative_base
3
+ from sqlalchemy .orm import relationship , sessionmaker
4
+ from sqlalchemy import create_engine
5
+
6
+ # 1. Create an engine that stores data in a address_alchemy.db file in the local directory
7
+ # The engine establishes a DBAPI connection to the database when a method like execute() or connect() is called
8
+ engine = create_engine ('sqlite:///address_sqlalchemy.db' )
9
+
10
+ # 2. Describe the model (i.e. the database tables and the classesmapped to those)
11
+
12
+ # Create the declarative base class. This stores a catlog of classes and mapped tables using the Declarative system
13
+ Base = declarative_base ()
14
+
15
+ # Define the classes and tables
16
+ class Person (Base ):
17
+ __tablename__ = 'person'
18
+ person_id = Column (Integer , primary_key = True )
19
+ first_name = Column (String (250 ), nullable = False )
20
+ last_name = Column (String (250 ), nullable = False )
21
+ address = relationship ("Address" )
22
+
23
+ def __repr__ (self ):
24
+ return "<Person(id='%s', first_name='%s', last name='%s')>" % (self .person_id , self .first_name , self .last_name )
25
+
26
+
27
+ class Address (Base ):
28
+ __tablename__ = 'address'
29
+ address_id = Column (Integer , primary_key = True )
30
+ street_name = Column (String (120 ))
31
+ street_number = Column (String (6 ))
32
+ post_code = Column (String (10 ))
33
+ person_id = Column (Integer , ForeignKey ('person.person_id' ))
34
+
35
+ def __repr__ (self ):
36
+ return "<Address('%s', '%s', '%s)>" % (
37
+ self .street_number , self .street_name , self .post_code )
38
+
39
+
40
+ # 3. Create all tables
41
+ Base .metadata .create_all (engine )
42
+
43
+ # 4. Create a session object to allow us to interact with the database
44
+ # Session class is defined using sessionmaker() – a configurable session factory method which is bound to the engine
45
+ # object created earlier.
46
+ Session = sessionmaker (bind = engine )
47
+ session = Session ()
48
+
49
+ # 5. Create people objects then add to the database using the session object
50
+ p1 = Person (first_name = 'R2' , last_name = 'D2' )
51
+ session .add (p1 )
52
+
53
+ session .add_all ([
54
+ Person (first_name = 'Metal' , last_name = 'Mickey' ),
55
+ Person (first_name = 'Marvin' , last_name = 'Paranoid-Android' ),
56
+ Person (first_name = 'K' , last_name = '9' )]
57
+ )
58
+
59
+ # 4. Create address objects and add to the database
60
+ # Add a person and their address
61
+ et = Person (first_name = 'E.' , last_name = 'T.' )
62
+ # Address has to be a list even if there is only one address
63
+ et .address = [Address (street_number = '1A' , street_name = 'Brodo Asogi' , post_code = '' )]
64
+ session .add (et )
65
+
66
+ # Add an address for an existing person
67
+ p2 = session .query (Person ).filter_by (first_name = 'Metal' ).first ()
68
+ p2 .address = [Address (street_number = '34' , street_name = 'Wilberforce Street' , post_code = 'B4 7ET' )]
69
+
70
+ # 5. Commit the changes to the database
71
+ session .commit ()
72
+
73
+ # 6. Close the session
74
+ session .close ()
0 commit comments