现在来设计一个能描述“图书”与“作者”的关系的表结构,需求是
- 一本书可以有好几个作者一起出版
- 一个作者可以写好几本书
1 #!/usr/bin/env python 2 from sqlalchemy import Table,Column,Integer,String,DATE,ForeignKey 3 from sqlalchemy.orm import relationship 4 from sqlalchemy.ext.declarative import declarative_base 5 from sqlalchemy import create_engine 6 from sqlalchemy.orm import sessionmaker 7 8 9 engine = create_engine("mysql+pymysql://root:root@localhost/testuser?charset=utf8",encoding="utf-8",echo=True) 10 # 生成orm基类 11 Base = declarative_base() 12 13 14 # 创建表映射 15 book_m2m_author = Table("book_m2m_author",Base.metadata, 16 Column("book_id",Integer,ForeignKey("books.id")), 17 Column("author_id",Integer,ForeignKey("authors.id"))) 18 19 class Book(Base): 20 __tablename__ = "books" 21 id = Column(Integer,primary_key=True) 22 name = Column(String(64)) 23 pub_date = Column(DATE) 24 authors = relationship("Author",secondary=book_m2m_author,backref="books") 25 26 def __repr__(self): 27 return self.name 28 29 class Author(Base): 30 __tablename__ = "authors" 31 id = Column(Integer,primary_key=True) 32 name = Column(String(32)) 33 34 def __repr__(self): 35 return self.name 36 37 # 构建表结构 38 Base.metadata.create_all(engine)
#!/usr/bin/env python from day12 import orm_m2m_fk from sqlalchemy.orm import sessionmaker # 创建与数据库会话实例。 session_class = sessionmaker(bind=orm_m2m_fk.engine) # 生成session实例 session = session_class() # b1 = orm_m2m_fk.Book(name="Java从入门到精通",pub_date="2018-08-08") # b2 = orm_m2m_fk.Book(name="Python从入门到精通",pub_date="2018-08-08") # b3 = orm_m2m_fk.Book(name="JavaScript从入门到精通",pub_date="2018-08-08") # b4 = orm_m2m_fk.Book(name=".Net从入门到精通",pub_date="2018-08-08") # # # a1 = orm_m2m_fk.Author(name="Jerry") # a2 = orm_m2m_fk.Author(name="Tom") # a3 = orm_m2m_fk.Author(name="Jack") # # b1.authors = [a1,a3] # # b3.authors = [a1,a2,a3] # # b2.authors = [a2,a3] # # b4.authors = [a1] # # session.add_all([b1,b2,b3,b4,a1,a2,a3]) a = session.query(orm_m2m_fk.Author).filter(orm_m2m_fk.Author.name=="Jerry").first() print(a.books) session.commit()
1 C:UsersAdministratorAppDataLocalProgramsPythonPython37python.exe D:/PythonStudy/charm/01/day12/orm_m2m_aip.py 2 2018-09-20 09:22:14,652 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode' 3 2018-09-20 09:22:14,652 INFO sqlalchemy.engine.base.Engine {} 4 2018-09-20 09:22:14,656 INFO sqlalchemy.engine.base.Engine SELECT DATABASE() 5 2018-09-20 09:22:14,656 INFO sqlalchemy.engine.base.Engine {} 6 2018-09-20 09:22:14,656 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8mb4' and `Collation` = 'utf8mb4_bin' 7 2018-09-20 09:22:14,656 INFO sqlalchemy.engine.base.Engine {} 8 C:UsersAdministratorAppDataLocalProgramsPythonPython37libsite-packagespymysqlcursors.py:170: Warning: (1366, "Incorrect string value: '\xD6\xD0\xB9\xFA\xB1\xEA...' for column 'VARIABLE_VALUE' at row 484") 9 result = self._query(query) 10 2018-09-20 09:22:14,658 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1 11 2018-09-20 09:22:14,658 INFO sqlalchemy.engine.base.Engine {} 12 2018-09-20 09:22:14,658 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1 13 2018-09-20 09:22:14,658 INFO sqlalchemy.engine.base.Engine {} 14 2018-09-20 09:22:14,659 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin AS anon_1 15 2018-09-20 09:22:14,659 INFO sqlalchemy.engine.base.Engine {} 16 2018-09-20 09:22:14,660 INFO sqlalchemy.engine.base.Engine DESCRIBE `book_m2m_author` 17 2018-09-20 09:22:14,660 INFO sqlalchemy.engine.base.Engine {} 18 2018-09-20 09:22:14,661 INFO sqlalchemy.engine.base.Engine DESCRIBE `books` 19 2018-09-20 09:22:14,661 INFO sqlalchemy.engine.base.Engine {} 20 2018-09-20 09:22:14,663 INFO sqlalchemy.engine.base.Engine DESCRIBE `authors` 21 2018-09-20 09:22:14,663 INFO sqlalchemy.engine.base.Engine {} 22 2018-09-20 09:22:14,668 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) 23 2018-09-20 09:22:14,668 INFO sqlalchemy.engine.base.Engine SELECT authors.id AS authors_id, authors.name AS authors_name 24 FROM authors 25 WHERE authors.name = %(name_1)s 26 LIMIT %(param_1)s 27 2018-09-20 09:22:14,669 INFO sqlalchemy.engine.base.Engine {'name_1': 'Jerry', 'param_1': 1} 28 2018-09-20 09:22:14,670 INFO sqlalchemy.engine.base.Engine SELECT books.id AS books_id, books.name AS books_name, books.pub_date AS books_pub_date 29 FROM books, book_m2m_author 30 WHERE %(param_1)s = book_m2m_author.author_id AND books.id = book_m2m_author.book_id 31 2018-09-20 09:22:14,670 INFO sqlalchemy.engine.base.Engine {'param_1': 1} 32 [Java从入门到精通, JavaScript从入门到精通, .Net从入门到精通] 33 2018-09-20 09:22:14,675 INFO sqlalchemy.engine.base.Engine COMMIT 34 35 Process finished with exit code 0