突突突突突突

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
Base = declarative_base()


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

    # 创建关系  secondary 使用表名!!!
    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'))

# 4.创建数据库连接
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:root@127.0.0.1:3306/my_sqlalchemy?charset=utf8')

# 5.
# 去engine数据库中创建所有继承Base类的ORM对象
Base.metadata.create_all(engine)

 

posted on 2019-02-15 20:18  徐建0304  阅读(214)  评论(0编辑  收藏  举报