时间:2021-07-01 10:21:17 帮助过:8人阅读
外键关联之双向反查:
from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column,String,Integer from sqlalchemy.orm import sessionmaker,relationship from sqlalchemy import ForeignKey engine = create_engine("mysql+pymysql://root:mysql8@localhost/mysqltest") Base = declarative_base() # 学员表student跟考勤表study_record是一对多的关系 class Student(Base): __tablename__ = ‘student‘ id = Column(Integer, primary_key=True) name = Column(String(32)) sex = Column(String(32)) def __repr__(self): return "%s,%s,%s" %(self.id, self.name, self.sex) class Study_record(Base): __tablename__ = ‘study_record‘ id = Column(Integer, primary_key=True) day = Column(Integer) status = Column(String(32)) stu_id = Column(Integer, ForeignKey(‘student.id‘)) # 双向反查关系,通过student字段可以看记录对应的学生,反过来通过my_study_record可看学生对应的记录 student = relationship("Student", backref="my_study_record") def __repr__(self): # self.student.name 这是它的牛逼之处: return "%s,%s,%s" %(self.day, self.status, self.student.name) Session_class = sessionmaker(bind=engine) session = Session_class() # 上面加了外键关联,可以用了 result1 = session.query(Student).join(Study_record, isouter=True).all() print(result1) # [1,alex,男, 2,jack,男, 3,rose,女] alex = session.query(Student).filter(Student.name == ‘alex‘).first() print(alex) # 1,alex,男 # 如果想查Alex的上课记录咋查呢?在上面加一个双向反查关系 print(alex.my_study_record) # [1,YES,alex, 2,NO,alex]
顾客表有两个外键,两个外键指向同一张表,此时的双向查询:
from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column,String,Integer from sqlalchemy.orm import relationship from sqlalchemy import ForeignKey engine = create_engine("mysql+pymysql://root:mysql8@localhost/mysqltest") Base = declarative_base() # 顾客表Customer有两个外键,订单地址ID,邮寄地址ID。都关联了同一张表 class Customer(Base): __tablename__ = ‘customer‘ id = Column(Integer, primary_key=True) name = Column(String(32)) bill_addr_id = Column(Integer, ForeignKey("address.id")) post_addr_id = Column(Integer, ForeignKey("address.id")) bill_addr = relationship("Address", foreign_keys=[bill_addr_id], backref="bill_customers") post_addr = relationship("Address", foreign_keys=[post_addr_id], backref="post_customers") def __repr__(self): return "%s" %(self.name) class Address(Base): __tablename__ = ‘address‘ id = Column(Integer, primary_key=True) detail_address = Column(String(32)) def __repr__(self): return "%s" %(self.detail_address) Base.metadata.create_all(engine)orms模块
from sqlalchemy.orm import sessionmaker import orms Session_class = sessionmaker(bind=orms.engine) session = Session_class() a1 = orms.Address(detail_address="BeiJing") a2 = orms.Address(detail_address="ShangHai") a3 = orms.Address(detail_address="TianJin") session.add_all([a1, a2, a3]) # 注意这里赋值的牛逼之处!!!直接用对象进行赋值 c1 = orms.Customer(name="Alex", bill_addr=a1, post_addr=a2) c2 = orms.Customer(name="Jack", bill_addr=a2, post_addr=a3) c3 = orms.Customer(name="Rain", bill_addr=a3, post_addr=a3) session.add_all([c1, c2, c3]) session.commit()init_data模块
from sqlalchemy.orm import sessionmaker import orms Session_class = sessionmaker(bind=orms.engine) session = Session_class() alex = session.query(orms.Customer).filter(orms.Customer.name=="Alex").first() print(alex.bill_addr, alex.post_addr) # BeiJing ShangHai sh = session.query(orms.Address).filter(orms.Address.detail_address=="ShangHai").first() print(sh.bill_customers) # [Jack]
sqlalchemy外键的一些东西
标签:div lap sed def fir 学生 分享图片 2-2 ring