1 #!/usr/bin/env python3.52 #-*-Coding:utf8-*-3 #Many-to-many associations4 fromSQLAlchemyImportTable, Column, Integer, Foreignkey,string,create_engine,and_,or_,func5 fromSqlalchemy.ormImportRelationship,sessionmaker6 fromSqlalchemy.ext.declarativeImportDeclarative_base7Base = Declarative_base ()#generates a Sqlorm base class8Engine = Create_engine ("Mysql+pymysql://python:[email Protected]:3306/python", echo=True)9 #1. Create an association table that associates two groups of objects and hostsTenHost2group = Table ('Host_2_group', Base.metadata, OneColumn ('host_id', ForeignKey ('hosts.id'), primary_key=True), AColumn ('group_id', ForeignKey ('group.id'), primary_key=True), - ) - the classHost (Base): - __tablename__='hosts' -id = Column (integer,primary_key=true,autoincrement=True) -hostname = Column (String), unique=true,nullable=False) +IP_ADDR = Column (String), unique=true,nullable=False) -Port = Column (integer,default=22) +Groups = Relationship ("Group", ASecondary=host2group,#specifying an instance of an intermediate table atBackref ='host_list')#The backref parameter provides a declaration of a reverse reference to the relationship. - def __repr__(self): - return "<id =%s,hostname=%s,ip_addr =%s>"% - (Self.id, - Self.hostname, - self.ip_addr) in classGroup (Base): - __tablename__="Group" toid = Column (integer,primary_key=True) +Name = Column (String), unique=true,nullable=False) - def __repr__(self): the return "<group_id =%s,group_name=%s>"% * (Self.id, $ self.name)Panax Notoginseng - theBase.metadata.create_all (Engine)#Create all table Structures + # A if __name__=='__main__': theSessioncls = Sessionmaker (bind=engine)#create a session with the database sessions class, note that this is a class that is returned to the session, not an instance +Session = SESSIONCLS ()#This is the instance of the connection. - #G1 = Group (name = "G1") $ #g2 = Group (name = "G2") $ #g3 = Group (name = "G3") - #G4 = Group (name = "G4") - " " the H1 = Host (hostname= ' H1 ', ip_addr= ' 192.168.2.242 ', port = $) - h2 = Host (hostname= ' H2 ', ip_addr= ' 192.168.2.243 ', port=20000)Wuyi h3 = Host (hostname= ' H3 ', ip_addr= ' 192.168.2.244 ', port=20000) the Session.add_all ([h1,h2,h3]) - " " Wu #Associated host - #Find a group AboutG1 =session.query (Group). First () $ #groups = Session.query (Group). All () - #H1 = Session.query (Host). Filter (Host.hostname = = "H1"). First () - #Find a host -H2 = Session.query (Host). Filter (Host.hostname = ="H2"). First () A #h3 = Session.query (Host). Filter (Host.hostname = = "H3"). First () + ## h1.groups = Groups the ## H1.groups.pop () - #Association to make a host belong to more than one group $ #h2.groups = groups[1:-1] the #Enquiry the Print("======>", h2.groups) the Print("=====>", G1.host_list) the #Update - #obj = session.query (Host). Filter (Host.hostname = = "Test_localhost"). First () in #print ("++>", obj) the ##obj. hostname = "Test_localhost" # change the #session.delete (obj) About #Objs = Session.query (Host). Filter (And_ (Host.hostname.like ("ubun%"), Host.port >). All () the #print ("+++>", Objs) the the +Session.commit ()
For relevant Sqlalchem reference documents click here >>>>>
SQLAlchemy Related knowledge