多外键关联
注:在两个表之间进行多外键链接
如图:
案例:
# 创建两张表并添加外键主键
# 调用Column创建字段 加类型 from sqlalchemy import Integer, ForeignKey, String, Column # 调用基类Base from sqlalchemy.ext.declarative import declarative_base # 调用操作链接,反查 from sqlalchemy.orm import relationship # 调用链接数据库 from sqlalchemy import create_engine Base = declarative_base() class Customer(Base): __tablename__ = 'customer' id = Column(Integer, primary_key=True) name = Column(String(64)) # 以Address表id字段 设置两个外键 billing_address_id = Column(Integer, ForeignKey("address.id")) shipping_address_id = Column(Integer, ForeignKey("address.id")) # relationship -允许你在Student表里通过backref字段反向查出所有它在表里的关联项- # foreign_keys=绑定外键 多次relationship 反差会识别不出指定数据所以添加foreign_keys参数。 billing_address = relationship("Address",foreign_keys=[billing_address_id]) shipping_address = relationship("Address",foreign_keys=[shipping_address_id]) class Address(Base): __tablename__ = 'address' id = Column(Integer, primary_key=True) street = Column(String(64)) city = Column(String(64)) state = Column(String(64)) # 返回一个可以用来表示对象的可打印字符串 def __repr__(self): return self.street # encoding='utf-8' 链接字符集 ,echo=True 把所有信息打印出来 engine = create_engine("mysql+pymysql://root:123456@192.168.1.100/xiang",encoding='utf-8', #echo=True ) #---------------1.创建表-----------------# # 创建表结构 Base.metadata.create_all(engine) #---------------------------------------#
# 插入表内字段数据,测试访问
from www import orm_fk from sqlalchemy.orm import sessionmaker # bind=engine 绑定engine socket实例 Session_class = sessionmaker(bind=orm_fk.engine) # 生成session实例,如同pymysql内的cursor Session = Session_class() #---------------2.写入数据-----------------# # 写入数据 # addr1 = orm_fk.Address(street="Tiantongyuan",city="ChangPing",state="BJ") # addr2 = orm_fk.Address(street="Wudaokou",city="Haidian",state="BJ") # addr3 = orm_fk.Address(street="Yanjiao",city="Langfang",state="HB") # 插入数据 # Session.add_all([addr1,addr2,addr3]) # 写入数据 并调用 Address字段插入数据 为 Customer数据 # c1 = orm_fk.Customer(name="kevin",billing_address=addr1,shipping_address=addr2) # c2 = orm_fk.Customer(name="Jack",billing_address=addr3,shipping_address=addr3) # 插入数据 # Session.add_all([c1,c2]) #---------------------------------------# #---------------3.查询数据-----------------# # 取出Customer内的 内存对象 obj = Session.query(orm_fk.Customer).filter(orm_fk.Customer.name=="kevin").first() # 通过内存 调用第一行name字段数据, # 调用billing_address对应外键id的对应数据, # 调用obj.shipping_address对应外键id的对应数据。 print(obj.name,obj.billing_address,obj.shipping_address) #---------------------------------------# # 执行事务 Session.commit()
# 表customer +----+-------+--------------------+---------------------+ | id | name | billing_address_id | shipping_address_id | +----+-------+--------------------+---------------------+ | 1 | kevin | 1 | 2 | | 2 | Jack | 3 | 3 | +----+-------+--------------------+---------------------+ # 表address +----+--------------+-----------+-------+ | id | street | city | state | +----+--------------+-----------+-------+ | 1 | Tiantongyuan | ChangPing | BJ | | 2 | Wudaokou | Haidian | BJ | | 3 | Yanjiao | Langfang | HB | +----+--------------+-----------+-------+