时间:2021-07-01 10:21:17 帮助过:12人阅读
插入数据,为了整体的简洁,数据操作在另一张表进行
1 # -*- coding: UTF-8 -*- 2 import multi_fk 3 from multi_fk import Customer 4 from multi_fk import Address 5 from sqlalchemy.orm import sessionmaker 6 7 # 创建session会话 8 Session_class = sessionmaker(bind=multi_fk.engine) 9 # 生成session实例 10 session = Session_class() 11 12 # 数据 13 address_obj1 = Address(street=‘daguanlu‘, city=‘hz‘, state=‘zj‘) 14 address_obj2 = Address(street=‘gudunlu‘, city=‘hz‘, state=‘zj‘) 15 address_obj3 = Address(street=‘xinjiekou‘, city=‘nj‘, state=‘js‘) 16 session.add_all([address_obj1,address_obj2,address_obj3]) 17 18 customer_obj1 = Customer(name="bigberg", billing_address=address_obj1, 19 shopping_address=address_obj2) 20 21 customer_obj2 = Customer(name="Jack", billing_address=address_obj3, 22 shopping_address=address_obj3) 23 24 session.add_all([customer_obj1,customer_obj2]) 25 26 session.commit()multi_fk_data
数据和表结构
mysql> select * from address; +----+-----------+------+-------+ | id | street | city | state | +----+-----------+------+-------+ | 1 | daguanlu | hz | zj | | 2 | gudunlu | hz | zj | | 3 | xinjiekou | nj | js | +----+-----------+------+-------+ 3 rows in set (0.00 sec) mysql> select * from customer; +----+---------+--------------------+---------------------+ | id | name | billing_address_id | shopping_address_id | +----+---------+--------------------+---------------------+ | 1 | bigberg | 1 | 2 | | 2 | Jack | 3 | 3 | +----+---------+--------------------+---------------------+ 2 rows in set (0.00 sec) mysql> desc address; +--------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | street | varchar(64) | NO | | NULL | | | city | varchar(64) | NO | | NULL | | | state | varchar(64) | NO | | NULL | | +--------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql> desc customer; +---------------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(32) | NO | | NULL | | | billing_address_id | int(11) | YES | MUL | NULL | | | shopping_address_id | int(11) | YES | MUL | NULL | | +---------------------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)View Code
查询
# -*- coding: UTF-8 -*- import multi_fk from multi_fk import Customer from multi_fk import Address from sqlalchemy.orm import sessionmaker # 创建session会话 Session_class = sessionmaker(bind=multi_fk.engine) # 生成session实例 session = Session_class() obj = session.query(Customer).filter(Customer.name==‘bigberg‘).first() print(obj.name,‘\n‘,‘bill_address:‘,obj.billing_address,‘\n‘, ‘shopping_address:‘, obj.shopping_address) session.commit() #输出 bigberg bill_address: 省份:zj 城市:hz 街区:daguanlu shopping_address: 省份:zj 城市:hz 街区:gudunlumulti_fk_query
sqlalchemy多外键关联
标签:ada sel add 实例 技术 null meta 连接 open