我使用TypeORM来处理一个类似于以下内容的实体:
@Entity('users')
export class UserEntity extends BaseEntity {
@PrimaryColumn()
id: string;
@CreateDateColumn({ type: 'timestamp' })
createdAt: Date;
@UpdateDateColumn({ type: 'timestamp' })
updatedAt: Date;
}
但是,当我试图使用timestamp
实体的存储库执行任何类型的与TypeORM等式相关的SQL查询时,它不能正常工作。例如,查询:
const id = 'dbe9e81d-aefa-459d-8460-707ade0fa156';
const userEntity = userRepository.findOne(id); // UserEntity(...)
const sameUserEntity = userRepository.findOne({ where: { createdAt: userEntity.createdAt } }); // undefined
返回userEntity
的正确实体和sameUserEntity
的未定义实体。我查看了TypeORM为该查询构造的日志,它如下所示:
SELECT "UserEntity"."id" AS "UserEntity_id",
"UserEntity"."created_at" AS "UserEntity_created_at",
"UserEntity"."updated_at" AS "UserEntity_updated_at"
FROM "users" "UserEntity"
WHERE "UserEntity"."created_at" = $1 LIMIT 1 -- PARAMETERS: ["2022-02-19T22:10:13.564Z"]
TypeORM似乎没有将JavaScript Date
对象转换为正确的PostgreSQL timestamp
格式。数据库中的时间戳看起来像2022-02-19 22:10:13.564432
,它是一种完全不同的格式,而且精度更高。
在使用timestamp
时,是否应该有一种特定的方式进行与TypeORM相关的搜索?
注意:我也尝试过寻找有同样问题的人,但我没有看到任何明确的解决方案。我试图围绕创建的日期实现基于游标的分页,但是大于和小于运算符也不能正常工作。
发布于 2022-04-13 11:59:56
最近,我遇到了同样的问题,并通过将precision: 3
添加到列装饰器来修复它。请注意,这是基于这样的假设,即您开始不需要这样的精度。
@Entity('users')
export class UserEntity extends BaseEntity {
@PrimaryColumn()
id: string;
@CreateDateColumn({
type: 'timestamp',
precision: 3
})
createdAt: Date;
@UpdateDateColumn({
type: 'timestamp',
precision: 3
})
updatedAt: Date;
}
https://stackoverflow.com/questions/71189866
复制相似问题