#Auther nmap import orm_m2m from sqlalchemy.orm import sessionmaker #插入数据 Session_class = sessionmaker( bind=orm_m2m.engine) #创建与数据库的会话session class ,注意,这里返回给session的是个class,不是实例 Session = Session_class() #生成session实例 #可以理解为打开游标cursor # b1 = orm_m2m.Book(name="book1") # b2 = orm_m2m.Book(name="book2") # b3 = orm_m2m.Book(name="book3") # b4 = orm_m2m.Book(name="book4") b5 = orm_m2m.Book(name="book4哈哈哈") # a1 = orm_m2m.Author(name="nmap") # a2 = orm_m2m.Author(name="Jack") # a3 = orm_m2m.Author(name="Rain") # # b1.authors = [a1,a2] # b2.authors = [a1,a2,a3] # Session.add_all([b1,b2,b3,b4,a1,a2,a3]) Session.add_all([ b5, ]) # print('--------通过书表查关联的作者---------') # # book_obj = Session.query(orm_m2m.Book).filter(orm_m2m.Book.name=="book1").first() # print(book_obj) # print(book_obj.name, book_obj.authors) # # print('--------通过作者表查关联的书---------') # author_obj =Session.query(orm_m2m.Author).filter(orm_m2m.Author.name=="nmap").first()
import orm_m2m from sqlalchemy.orm import sessionmaker Session = sessionmaker(bind=orm_m2m.engine) session = Session() author0 = orm_m2m.Author(name="lieon0") author1 = orm_m2m.Author(name="lieon1") author2 = orm_m2m.Author(name="lieon2") author3 = orm_m2m.Author(name="lieon3") book0 = orm_m2m.Book(name="asdf", authors=[author0, author1]) book1 = orm_m2m.Book(name="we", authors=[author1, author2]) book2 = orm_m2m.Book(name="ew", authors=[author1, author3]) # session.add_all([author0, author1, author2, author3]) # session.add_all([book0, book1, book2]) author_obj = session.query(orm_m2m.Author).filter_by(name="lieon0").first() print(author_obj, author_obj.books) book_obj = session.query(orm_m2m.Book).filter_by(id=3).first() print(book_obj, book_obj.authors) book_obj.authors.remove() session.commit()
#!/usr/bin/python3 #encoding:utf-8 #@Author:CaiDeyang #@Time: 2018/8/1 16:11 from sqlalchemy.orm import sessionmaker import orm_m2m Session_class = sessionmaker( bind=orm_m2m.engine) #创建与数据库的会话session class ,注意,这里返回给session的是个class,不是实例 session = Session_class() #生成session实例 b1 = orm_m2m.Book(name="跟Alex学Python") #新增书籍数据 b2 = orm_m2m.Book(name="跟Alex学把妹") b3 = orm_m2m.Book(name="跟Alex学装逼") b4 = orm_m2m.Book(name="跟Alex学开车") a1 = orm_m2m.Author(name="Alex") #新增作者数据 a2 = orm_m2m.Author(name="Jack") a3 = orm_m2m.Author(name="Rain") b1.authors = [a1, a2] #创建数据与组着的对应关系 b2.authors = [a1, a2, a3] # session.add_all([b1,b2,b3,b4,a1,a2,a3]) #添加数据 # session.commit() print('--------通过书表查关联的作者---------') book_obj = session.query( orm_m2m.Book).filter(orm_m2m.Book.name == "跟Alex学Python").first() print(book_obj.name, book_obj.authors) #跟Alex学Python [Alex, Jack] print('--------通过作者表查关联的书---------') auth_obj = session.query( orm_m2m.Author).filter(orm_m2m.Author.name == "Alex").first() print(auth_obj.name, auth_obj.books) #Alex [跟Alex学Python, 跟Alex学把妹]
# G2 = orm_m2m.Groups(name="Guest") # # Session.add_all([A1,A2,G1,G2]) # Session.commit() # U1 = orm_m2m.Userss(name="huang",group=1,address=2) # Session.add(U1) # Session.commit() # # a1 = orm_m2m.C_Address(name="hunan") # a2 = orm_m2m.C_Address(name="hubei") # c1 = orm_m2m.Customer(name="shopp",bi_address=1,si_address=1) # Session.add_all([a1,a2,c1]) # Session.commit() b1 = orm_m2m.Book(name="python",pub_data="2017-01-01") b2 = orm_m2m.Book(name="Linux",pub_data="2016-01-01") a1 = orm_m2m.Author(name="junly") a2 = orm_m2m.Author(name="huang") # b1.authors(a1) # b2.authors(a1,a2) a1.book_2_author(b1) a2.book_2_author(b1,b2) Session.add_all([b1,b2,a1,a2]) Session.commit()
#!/usr/bin/env python # coding:utf-8 """ Name : orm_m2m_api.py Author : anne Time : 2019-09-05 11:59 Desc: """ import orm_m2m from sqlalchemy.orm import sessionmaker Session_class = sessionmaker( bind=orm_m2m.engine) #创建与数据库的会话session class,注意返回给session的是个class不是实例 Session = Session_class() #生成session实例 #类似与cursor b1 = orm_m2m.Book(name="跟Alex学Python", pub_date='2018-09-09') b2 = orm_m2m.Book(name="跟Alex学把妹", pub_date='2018-07-09') b3 = orm_m2m.Book( name="跟Alex学装逼", pub_date='2018-06-09', ) a1 = orm_m2m.Author(name="Alex") a2 = orm_m2m.Author(name="Jack") a3 = orm_m2m.Author(name="Rain") b1.authors = [a1, a2] b2.authors = [a1, a2, a3] Session.add_all([b1, b2, b3, a1, a2, a3]) author_obj = Session.query(