You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
const { DataTypes } = require ( 'sequelize' ) ;
// 导入数据库连接实例
const { sequelize } = require ( '../config/db' ) ;
// 定义 User 模型(仅映射已手动创建的 users 表,不自动建表)
const User = sequelize . define ( 'User' , {
// 字段1: id( 对应表中 id 字段,主键自增)
id : {
type : DataTypes . INTEGER ,
primaryKey : true ,
autoIncrement : true ,
comment : '用户ID'
} ,
// 字段2: username( 对应表中 username 字段,非空唯一)
username : {
type : DataTypes . STRING ( 50 ) ,
allowNull : false ,
unique : true ,
comment : '用户名(登录用)'
} ,
// 字段3: password( 对应表中 password 字段,非空)
password : {
type : DataTypes . STRING ( 100 ) ,
allowNull : false ,
comment : '用户密码'
} ,
// 字段4: email( 对应表中 email 字段,可选唯一)
email : {
type : DataTypes . STRING ( 100 ) ,
unique : true ,
allowNull : true ,
comment : '用户邮箱'
} ,
// 字段5-6: createdAt/updatedAt( 对应表中这两个字段, 自动维护, 无需手动赋值)
createdAt : {
type : DataTypes . DATE ,
allowNull : false ,
comment : '创建时间'
} ,
updatedAt : {
type : DataTypes . DATE ,
allowNull : false ,
comment : '更新时间'
}
} , {
// 模型配置:关键是关闭自动建表相关逻辑,仅做表映射
tableName : 'users' , // 明确指定映射的表名(必须和手动创建的表名一致)
timestamps : true , // 开启自动维护 createdAt/updatedAt( 表中已手动创建这两个字段)
paranoid : false , // 关闭软删除(和表结构匹配)
charset : 'utf8mb4' ,
collate : 'utf8mb4_unicode_ci' ,
freezeTableName : true , // 冻结表名,避免 Sequelize 自动给表名加复数(这里表名已手动设为 users, 无需处理)
syncOnAssociation : false // 关闭关联自动同步(避免模型操作影响表结构)
} ) ;
// 导出模型,供控制器调用(后续登录接口查数据用)
module . exports = User ;