#!/usr/bin/env python # -*- coding:utf-8 -*- from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy import create_engine Base = declarative_base() # 创建单表 class Users(Base): __tablename__ = ‘users‘ id = Column(Integer, primary_key=True,autoincrement=True) name = Column(String(32)) extra = Column(String(16)) # __table_args__ = ( # UniqueConstraint(‘id‘, ‘name‘, name=‘uix_id_name‘), # Index(‘ix_id_name‘, ‘name‘, ‘extra‘), # ) class Phone(Base): __tablename__ = ‘Phone‘ pid = Column(Integer, primary_key=True) name = Column(String(32)) person = relationship(‘Person‘, backref=‘person‘) def init_db(): engine = create_engine("mysql+pymysql://root:qq995405033@127.0.0.1:3306/web", max_overflow=5) Base.metadata.create_all(engine) def drop_db(): engine = create_engine("mysql+pymysql://root:qq995405033@127.0.0.1:3306/web", max_overflow=5) Base.metadata.drop_all(engine) if __name__ == ‘__main__‘: drop_db() init_db()
原文:https://www.cnblogs.com/wanghong1994/p/13742270.html