sqlalchemy 相关
from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class User(Base): """ Eventually should be used for security purposes """ __tablename__ = "user" id = Column(Integer, primary_key=True) username = Column(String(ID_LEN), unique=True) email = Column(String(500)) def __init__(self, username=None, email=None): self.username = username self.email = email Base.metadata.create_all(settings.engine)
declarative_base() 创建了一个 BaseModel 类,这个类的子类可以自动与一个表关联
BaseModel.metadata.create_all(engine) 会找到 BaseModel 的所有子类,并在数据库中建立这些表;drop_all() 则是删除这些表。