1. 程式人生 > >sqlalchemy多外鍵關聯

sqlalchemy多外鍵關聯

efault spa row 技術 guanl code ddr nullable 否則

一、前言

  如果有張表A的多個字段關聯另一張表B的一個字段,就如同一個客戶表的賬單地址和發貨地址,同時關聯地址表中的id字段。

二、事例

技術分享圖片
# -*- coding: UTF-8 -*-
from sqlalchemy import create_engine
from sqlalchemy import Integer, ForeignKey, String, Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship



engine 
= create_engine("mysql+pymysql://bigberg:[email protected]:3306/study", encoding="utf-8", ) # 連接數據庫,echo=True =>把所有的信息都打印出來 Base = declarative_base() # 生成orm基類 class Customer(Base): __tablename__ = customer id = Column(Integer, primary_key=True) name = Column(String(32), nullable=False)
# 多個外鍵關聯 billing_address_id = Column(Integer, ForeignKey("address.id")) shopping_address_id = Column(Integer, ForeignKey("address.id")) # foreign_keys 一定要加,否則會報錯 billing_address = relationship("Address",foreign_keys=[billing_address_id]) shopping_address = relationship("Address",foreign_keys=[shopping_address_id])
class Address(Base): __tablename__ = address id = Column(Integer, primary_key=True) street = Column(String(64), nullable=False) city = Column(String(64), nullable=False) state = Column(String(64), nullable=False) def __repr__(self): return "省份:%s 城市:%s 街區:%s" %(self.state, self.city, self.street) # 創建表 Base.metadata.create_all(engine)
multi_fk

插入數據,為了整體的簡潔,數據操作在另一張表進行

技術分享圖片
 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 街區:gudunlu
multi_fk_query

sqlalchemy多外鍵關聯