真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

sqlalchemy連表

fromsqlalchemy.ext.declarativeimportdeclarative_base
fromsqlalchemy importColumn,Integer,String,ForeignKey,UniqueConstraint,Index
fromsqlalchemy.ormimportsessionmaker,relationship
fromsqlalchemy importcreate_engine
engine=create_engine("MySQL+pymysql://root:123456@192.168.0.6/wupeiqi",max_overflow=1000,
                     encoding='utf-8',echo=False)#最大連接數(shù)1000
base=declarative_base()

classUsers(base):
    __tablename__='users'
    
id=Column(Integer,primary_key=True)
    name=Column(String(32))
    extra=Column(String(16))
    __table_args__=(
    UniqueConstraint('id','name',name='uix_id_name'),
        Index('ix_id_name','name','extra'),
    )

    def__repr__(self):
        return"%s-%s"%(self.id,self.name)

classFavor(base):
    __tablename__='favor'
    
nid=Column(Integer,primary_key=True)
    caption=Column(String(50),default='red',unique=True)
    def__repr__(self):
        return"%s-%s"%(self.nid,self.caption)

classPerson(base):
    __tablename__ ='person'
    
nid = Column(Integer,primary_key=True)
    name = Column(String(32),index=True,nullable=True)
    favor_id = Column(Integer, ForeignKey("favor.nid"))#設(shè)置favor_id為外鍵,與favor表的nid關(guān)聯(lián)
    # 與生成表結(jié)構(gòu)無關(guān),僅用于查詢方便
    favor = relationship("Favor",backref='pers')#在計算機內(nèi)存里Person與Favor建立關(guān)聯(lián),Person通過favor關(guān)鍵字查詢
    #Favor里表的內(nèi)容,而Favor可以通過關(guān)鍵字pers反查Person里表的信息
    def__repr__(self):
        return  "%s-%s"%(self.name,self.favor.caption)

# 多對多
classServerToGroup(base):
    __tablename__ ='servertogroup'
    
nid = Column(Integer,primary_key=True,autoincrement=True)
    server_id = Column(Integer, ForeignKey('server.id'))
    group_id = Column(Integer, ForeignKey('group.id'))
    group = relationship("Group",backref='s2g')
    server = relationship("Server",backref='s2g')

classGroup(base):
    __tablename__ ='group'
    
id = Column(Integer,primary_key=True)
    name = Column(String(64),unique=True,nullable=False)
    port = Column(Integer,default=22)
    # group = relationship('Group',secondary=ServerToGroup,backref='host_list')


classServer(base):
    __tablename__ ='server'

    
id = Column(Integer,primary_key=True,autoincrement=True)
    hostname = Column(String(64),unique=True,nullable=False)

definit_db():
    base.metadata.create_all(engine)


defdrop_db():
    base.metadata.drop_all(engine)


base.metadata.create_all(engine)#建表

Session=sessionmaker(bind=engine)
session=Session()
obj=Users(name="alex0",extra='sb')
session.add(obj)
session.add_all([
    Users(name="alex1",extra='sb'),
    Users(name='alex2',extra='sb'),
])

#連表
ret=session.query(Person,Favor).filter(Person.favor_id==Favor.nid).all()
print(ret)
ret1=session.query(Person).join(Favor).all()
print(ret1)

#session.commit()

創(chuàng)新互聯(lián)建站秉承實現(xiàn)全網(wǎng)價值營銷的理念,以專業(yè)定制企業(yè)官網(wǎng),網(wǎng)站設(shè)計制作、成都網(wǎng)站制作重慶小程序開發(fā),網(wǎng)頁設(shè)計制作,手機網(wǎng)站制作,全網(wǎng)整合營銷推廣幫助傳統(tǒng)企業(yè)實現(xiàn)“互聯(lián)網(wǎng)+”轉(zhuǎn)型升級專業(yè)定制企業(yè)官網(wǎng),公司注重人才、技術(shù)和管理,匯聚了一批優(yōu)秀的互聯(lián)網(wǎng)技術(shù)人才,對客戶都以感恩的心態(tài)奉獻自己的專業(yè)和所長。


網(wǎng)頁標題:sqlalchemy連表
網(wǎng)站URL:http://weahome.cn/article/ijpodc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部