python 之sqlalchemy many to many

來源:互聯網
上載者:User

標籤:

 1 # -*- coding: utf-8 -*- 2 """ 3 @author: zengchunyun 4 """ 5 from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey, Table 6 from sqlalchemy.orm import sessionmaker, relationship, backref 7 from sqlalchemy.ext.declarative import declarative_base 8 from sqlalchemy import create_engine 9 10 Base = declarative_base()11 engine = create_engine(‘mysql+pymysql://root:[email protected]:3306/day11‘,echo=True)12 13 14 15 class Association(Base):16     __tablename__ = ‘association‘17     left_id = Column(Integer, ForeignKey("left.id"), primary_key=True)18     right_id = Column(Integer, ForeignKey("right.id"), primary_key=True)19     extra_data = Column(String(50))20     child = relationship("Child", back_populates="parents")21     parent = relationship("Parent", back_populates="children")22 23 24 class Parent(Base):25     __tablename__ = ‘left‘26     id = Column(Integer, primary_key=True)27     children = relationship("Association", back_populates=‘parent‘)28 29 class Child(Base):30     __tablename__ = ‘right‘31     id = Column(Integer, primary_key=True)32     parents = relationship("Association", back_populates="child")33 34 35 Base.metadata.create_all(engine)36 37 DBSession = sessionmaker()38 DBSession.configure(bind=engine)39 session = DBSession()  # 開啟資料連線40 41 42 # 插入資料方式一43 # p = Parent()44 # c = Child()45 # a = Association(extra_data="ss")46 # a.parent = p47 # a.child = c48 # 插入資料方式二49 c = Child()50 a = Association(extra_data=‘dd‘)51 a.parent = Parent()52 c.parents.append(a)53 54 # 插入資料方式三55 # p = Parent()56 # a = Association(extra_data="some data")57 # a.child = Child()58 # p.children.append(a)59 #60 # for assoc in p.children:61 #     print(assoc.extra_data)62 #     print(assoc.child)63 64 65 session.add(a)66 session.commit()

 

 

第二種方式

上面的其它代碼不變,只修改relationship關係,效果是一樣的

 1 class Association(Base): 2     __tablename__ = ‘association‘ 3     left_id = Column(Integer, ForeignKey("left.id"), primary_key=True) 4     right_id = Column(Integer, ForeignKey("right.id"), primary_key=True) 5     extra_data = Column(String(50)) 6     child = relationship("Child", backref="parents") 7     parent = relationship("Parent", backref="children") 8  9 10 class Parent(Base):11     __tablename__ = ‘left‘12     id = Column(Integer, primary_key=True)13 14 class Child(Base):15     __tablename__ = ‘right‘16     id = Column(Integer, primary_key=True)

 

python 之sqlalchemy many to many

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.