fastapi-demo/app/database/models.py

40 lines
1.8 KiB
Python
Raw Normal View History

"""
SQLAlchemy 数据库模型
"""
from sqlalchemy import Column, Integer, String, Float, Boolean, DateTime, Text
from sqlalchemy.sql import func
from .base import Base
class User(Base):
"""用户表"""
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
username = Column(String(50), unique=True, index=True, nullable=False, comment="用户名")
email = Column(String(100), unique=True, index=True, nullable=False, comment="邮箱")
full_name = Column(String(100), nullable=True, comment="全名")
is_active = Column(Boolean, default=True, nullable=False, comment="是否激活")
created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")
def __repr__(self):
return f"<User(id={self.id}, username='{self.username}', email='{self.email}')>"
class Product(Base):
"""产品表"""
__tablename__ = "products"
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
name = Column(String(200), nullable=False, comment="产品名称")
description = Column(Text, nullable=True, comment="产品描述")
price = Column(Float, nullable=False, comment="价格")
stock = Column(Integer, default=0, nullable=False, comment="库存数量")
is_available = Column(Boolean, default=True, nullable=False, comment="是否可用")
created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")
def __repr__(self):
return f"<Product(id={self.id}, name='{self.name}', price={self.price})>"