Sequelize常见问题
# 时间自动格式化
**问题:**每次查询datetime的字段,显示出来都是这种格式
2021-04-24T08:10:35.000Z
1
**解决办法:**初始化的时候设置dialectOptions
参数
let sequelize = new Sequelize(
config.database,
config.user,
config.password,
{
host: config.host,
port: config.port,
dialect: 'mysql',
dialectOptions: {
dateStrings: true,
typeCast: true
},
timezone: '+08:00' // 改为标准时区
}
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 关联表排序
官方文档
// Will order by an associated model's created_at using an association object. (preferred method)
[Subtask.associations.Task, 'createdAt', 'DESC'],
// Will order by a nested associated model's created_at using association objects. (preferred method)
[Subtask.associations.Task, Task.associations.Project, 'createdAt', 'DESC'],
1
2
3
4
5
2
3
4
5
User.findById(uID, {
include: [
model: sequelize.models.userProfile
as: userProfile,
include: [
{
model: sequelize.models.userProfileImages,
as: 'profileImages',
}
],
order: [['profileImages','id', 'desc']]
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
上次更新: 2023/09/22, 16:54:32