时间:2021-07-01 10:21:17 帮助过:3人阅读
表间关系:
多对一:
1 from sqlalchemy import Column,Integer,String,ForeignKey 2 #from database import Base 3 from sqlalchemy.ext.declarative import declarative_base 4 from sqlalchemy import create_engine 5 from sqlalchemy.orm import scoped_session,sessionmaker,relationship 6 7 8 engine=create_engine(‘sqlite:///dan.db‘,echo=True) 9 db_session=scoped_session(sessionmaker(autocommit=False,autoflush=False,bind=engine)) 10 Base=declarative_base() 11 Base.query=db_session.query_property() 12 Session=sessionmaker() 13 #多对一 14 class Person(Base): 15 __tablename__=‘pe‘ 16 id =Column(Integer,primary_key=True) 17 name=Column(String(80),unique=True) 18 email=Column(String(100),unique=True) 19 son=relationship("Son",backref="persons") 20 son_id=Column(Integer,ForeignKey(‘son.id‘)) 21 def __init__(self,name,email,son_id): 22 self.name=name 23 self.email=email 24 self.son_id=son_id 25 26 def __repr__(self): 27 return ‘<Person %r>‘%(self.name) 28 29 class Son(Base): 30 31 __tablename__=‘son‘ 32 id =Column(Integer,primary_key=True) 33 #per_id=Column(Integer,ForeignKey(‘Person.id‘)) 34 age=Column(Integer,unique=True) 35 36 def __init__(self,age): 37 self.age=age 38 39 def __repr__(self): 40 return "<Son %r>" %self.age 41 42 Base.metadata.create_all(bind=engine)
在插入数据时,首先应该插入son的数据,然后通过会话add(),然后flush(),只有这样,son的数据才会产生id,这样才能把son_id传入Person中。最后统一commit()
一对多:
from sqlalchemy import Column,Integer,String,ForeignKey #from database import Base from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session,sessionmaker,relationship engine=create_engine(‘sqlite:///ye.db‘,echo=True) db_session=scoped_session(sessionmaker(autocommit=False,autoflush=False,bind=engine)) Base=declarative_base() Base.query=db_session.query_property() Session=sessionmaker() #多对一 class Person(Base): __tablename__=‘pe‘ id =Column(Integer,primary_key=True) name=Column(String(80),unique=True) email=Column(String(100),unique=True) son=relationship("Son",backref="person") def __init__(self,name,email): self.name=name self.email=email #self.son_id=son_id def __repr__(self): return ‘<Person %r>‘%(self.name) class Son(Base): __tablename__=‘son‘ id =Column(Integer,primary_key=True) age=Column(Integer,unique=False)
#外键需要关联其他表的id person_id=Column(Integer,ForeignKey(‘pe.id‘)) def __init__(self,age,person_id): self.age=age self.person_id=person_id def __repr__(self): return "<Son %r>" %self.age Base.metadata.create_all(bind=engine)
一对一(以下都不再详细举例了):
只要在relationship函数中加上uselist=False属性即可,系统会自动判断
class Parent(Base): __tablename__ = ‘parent‘ id = Column(Integer, primary_key=True) child_id = Column(Integer, ForeignKey(‘child.id‘)) child = relationship("Child", backref=backref("parent", uselist=False)) class Child(Base): __tablename__ = ‘child‘ id = Column(Integer, primary_key=True)
多对多(通过第三张表连接):
association_table = Table(‘association‘, Base.metadata, Column(‘left_id‘, Integer, ForeignKey(‘left.id‘)), Column(‘right_id‘, Integer, ForeignKey(‘right.id‘)) ) class Parent(Base): __tablename__ = ‘left‘ id = Column(Integer, primary_key=True) children = relationship("Child", secondary=association_table, backref="parents") class Child(Base): __tablename__ = ‘right‘ id = Column(Integer, primary_key=True)
查询操作:
k=db_session.query(Person).filter(Person.name==‘wu‘).first()
a=db_session.query(Son).filter(Son.age==32).all()
k是一个对象,a则是一个迭代器,
关联表的查询:a=db_session.query(Person).join(Son).filter(Son.age==32).all()。
query()中的对象就是想要查询的对象
如:query(Person)---->返回Person对象
query(Person.name)----->返回Person.name的数据
flask_sqlalchemy笔记1
标签: