zoukankan      html  css  js  c++  java
  • sqlalchemy 多对多关系

    # -*- coding: utf-8 -*-
    
    from sqlalchemy import Column, String, create_engine,ForeignKey,Text,Integer,Table
    from sqlalchemy.orm import sessionmaker,relationship
    from sqlalchemy.ext.declarative import declarative_base
    
    # 创建对象的基类:
    Base = declarative_base()
    # 初始化数据库连接:
    engine = create_engine('mysql+pymysql://root:root@localhost:3306/test')
    # # 创建DBSession类型:
    # DBSession = sessionmaker(bind=engine)
    
    # Table(
    #     'mapping',
    #     Base.metadata,
    #     Column('id',Integer,autoincrement=True,primary_key=True),
    #     Column('recipe_id',Integer,ForeignKey('recipe.id')),
    #     Column('ingredient_id',Integer,ForeignKey('ingredient.id'))
    # )
    
    arctire_tag = Table(
        "arctire_tag",
        Base.metadata,
        Column('arctire_id',Integer,ForeignKey('arctire.id'),primary_key=True),
        Column("tag_id",Integer , ForeignKey("tag.id"),primary_key=True),
    )
    
    class Arctire(Base):
        __tablename__ = "arctire"
        id = Column(Integer , primary_key=True , autoincrement=True)
        name = Column(String(50) , nullable=False)
        tags = relationship("Tag", backref="arctires", secondary='arctire_tag')
    
        def __repr__(self):
            return "name:%s"%self.name
    
    class Tag(Base):
        __tablename__ = 'tag'
        id = Column(Integer , primary_key=True , autoincrement=True)
        tag = Column(String(50) , nullable=False)
    
        def __repr__(self):
            return "tag:%s"%self.tag
    
    Base.metadata.create_all(engine)
    

      

    # -*- coding: utf-8 -*-
    
    from sqlalchemy import Column, String, create_engine,ForeignKey,Text,Integer,Table
    from sqlalchemy.orm import sessionmaker,relationship
    from sqlalchemy.ext.declarative import declarative_base
    
    # 创建对象的基类:
    Base = declarative_base()
    # 初始化数据库连接:
    engine = create_engine('mysql+pymysql://root:root@localhost:3306/test')
    # # 创建DBSession类型:
    # DBSession = sessionmaker(bind=engine)
    
    # arctire_tag = Table(
    #     "arctire_tag",
    #     Base.metadata,
    #     Column('arctire_id',Integer,ForeignKey('arctire.id'),primary_key=True),
    #     Column("tag_id",Integer , ForeignKey("tag.id"),primary_key=True),
    # )
    
    class Arctire_tag(Base):
        __tablename__='arctire_tag'
        arctire_id=Column(Integer,ForeignKey('arctire.id'),primary_key=True)
        tag_id=Column(Integer,ForeignKey('tag.id'),primary_key=True)
    
    
    
    class Arctire(Base):
        __tablename__ = "arctire"
        id = Column(Integer , primary_key=True , autoincrement=True)
        name = Column(String(50) , nullable=False)
        tags = relationship("Tag", backref="arctires", secondary='arctire_tag')
    
        def __repr__(self):
            return "name:%s"%self.name
    
    class Tag(Base):
        __tablename__ = 'tag'
        id = Column(Integer , primary_key=True , autoincrement=True)
        tag = Column(String(50) , nullable=False)
    
        def __repr__(self):
            return "tag:%s"%self.tag
    
    Base.metadata.create_all(engine)
    

      

  • 相关阅读:
    ASM ClassReader failed to parse class file- probably due to a new Java class file version that isn't supported yet问题
    Git配置工作
    Git多人协作
    Git分支合并
    网络收藏夹
    莫道桑榆晚,为霞尚满天
    2021幼升小
    2020年07月28日10:36:36
    JAVA中如果用BigDecimal做除法
    mybatis
  • 原文地址:https://www.cnblogs.com/pythonClub/p/9851873.html
Copyright © 2011-2022 走看看