# -*- 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)