首页 > 其他 > 详细

多对多表间关系

时间:2019-05-18 19:27:31      阅读:118      评论:0      收藏:0      [点我收藏+]

创建表结构:

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship


class Girls(Base):
    __tablename__ = "girl"
    id = Column(Integer, primary_key=True)
    name = Column(String(32))

    #创建关系
    girl2boy = relationship("Boys",secondary="hotel",backref="boy2girl")


class Boys(Base):
    __tablename__ = "boy"
    id = Column(Integer, primary_key=True)
    name = Column(String(32))


class Hotel(Base):
    __tablename__ = "hotel"
    id = Column(Integer, primary_key=True)
    girl_id = Column(Integer, ForeignKey("girl.id"))
    boy_id = Column(Integer, ForeignKey("boy.id"))

from sqlalchemy import create_engine
engine = create_engine("mysql+pymysql://root:@127.0.0.1:3306/day127?charset=utf8")

Base.metadata.create_all(engine)

 secondary:指定关系表

多对多表间关系

原文:https://www.cnblogs.com/wen-kang/p/10886656.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!