from sqlalchemy.orm import declarative_base from db import engine from sqlalchemy import Table, Column, Integer, String, Enum, DATE, MetaData Base = declarative_base() # 创建表结构 -- 声名式 class User(Base): __tablename__ = 'user' id = Column(Integer, primary_key=True) name = Column(String(32)) password = Column(String(64)) def __repr__(self): return "<%s name:%s>" % (self.id, self.name) class Student(Base): __tablename__ = 'student' id = Column(Integer, primary_key=True) name = Column(String(32), nullable=False) gender = Column(Enum('m', 'f'), nullable=False) date = Column(DATE, nullable=False) def __repr__(self): return f"<{self.id}-{self.name}-{self.gender}-{self.date}>" Base.metadata.create_all(engine) # 创建表结构 -- 命令式 metadata = MetaData() # 生成metadata user = Table( 'user', metadata, Column('id', Integer, primary_key=True), Column('name', String(32)), Column('password', String(64)) ) student = Table( 'student', metadata, Column('id', Integer, primary_key=True), Column('name', String(32), nullable=False), Column('gender', Enum('m', 'f'), nullable=False), Column('date', DATE, nullable=False) )