初次代码
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
# model/users.py
|
||||
# 本文件定义 User 表的结构,映射到 MySQL 数据库
|
||||
|
||||
from sqlalchemy import Column, Integer, String, DateTime
|
||||
from sqlalchemy.sql import func
|
||||
from database import Base
|
||||
|
||||
class User(Base):
|
||||
"""
|
||||
用户表模型
|
||||
对应 MySQL 中的 users 表
|
||||
"""
|
||||
__tablename__ = "users" # 表名
|
||||
|
||||
# 字段定义
|
||||
id = Column(Integer, primary_key=True, index=True) # 主键,自增,索引
|
||||
username = Column(String(50), unique=True, index=True, nullable=False) # 用户名,唯一,非空
|
||||
email = Column(String(100), unique=True, index=True, nullable=False) # 邮箱,唯一,非空
|
||||
full_name = Column(String(100), nullable=True) # 全名,可为空
|
||||
|
||||
# created_at:创建时间,自动设置为当前时间(服务器时间)
|
||||
# server_default=func.now() 表示由数据库生成默认值
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
# updated_at:更新时间,当记录更新时自动设置为当前时间(由 SQLAlchemy 的 onupdate 触发)
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
Reference in New Issue
Block a user