typeorm存入时间晚了8个小时
来源:8-9 面向切面编程:TypeORM实现用户的CURD操作

qq_一曲丶终了_0
2023-11-01
使用typeorm的@CreateDateColumn/@UpdateDateColumn;自动存入的时间实际晚于现在8个小时; 以下是我的配置项,麻烦老师看一下哈; 我的版本:
@nestjs/typeorm版本为10.0.0, typeorm版本为0.3.17
// app.module.ts
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
timezone: 'Asia/Shanghai', // 日期配置
type: configService.get(ConfigEnum.DB_TYPE),
host: configService.get(ConfigEnum.DB_HOST),
port: configService.get(ConfigEnum.DB_PORT),
username: configService.get(ConfigEnum.DB_USERNAME),
password: configService.get(ConfigEnum.DB_PASSWORD),
database: configService.get(ConfigEnum.DB_DATABASE),
entities: [User, Article, Logs],
synchronize: configService.get(ConfigEnum.DB_SYNC) // 同步本地的schema与数据库 -> 初始化的时候使用(不能被用于生产环境,否则您可能会丢失生产环境数据)
} as TypeOrmModuleOptions)
})
// user.entity
import { Article } from "src/article/article.entity";
import { Logs } from "src/logs/logs.entity";
import { Column, CreateDateColumn, Entity, OneToMany, PrimaryGeneratedColumn, UpdateDateColumn, } from "typeorm";
@Entity()
export class User {
// 声明主键
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ type: 'varchar', length: 100, comment: '账户名称' })
accountName: string;
@Column({ type: 'varchar', length: 20, comment: '用户名' })
username: string;
@Column({ type: 'varchar', comment: '登录密码' })
password: string;
@OneToMany(() => Logs, (logs) => logs.user)
logs: Logs[];
@OneToMany(() => Article, (articles) => articles.user)
articles: Article[];
@CreateDateColumn({ type: 'timestamp', comment: '创建时间' })
createdAt: Date;
@UpdateDateColumn({ type: 'timestamp', comment: '更新时间' })
updatedAt: Date;
}
数据库配置(DBeaver):
查询结果:
我在数据库中使用SELECT now()发现依旧是晚了8个小时
期望: updatedAt和createdAt展示的时间应该与现在时间保持一致。
写回答
1回答
-
可以试一试在 docker-compose.yml 中设置 mysql 的时区
db:image: mysql8
......
environment:
TZ: Asia/Shanghai
然后执行 docker-compose up -d 重启容器
112023-11-01
相似问题