TypeORM 错误地插入日期 - 使用 Microsoft SQL Server

问题描述 投票:0回答:1

我在使用 Microsoft SQL Server (mssql v6.3.1)时遇到了 TypeORM

(v0.2.45) 的奇怪问题

我有一个Entity,其中有date

类型的列,当插入数据时,会从日期中减去一天:
@Entity("employees")
export class Employees extends BaseEntity {
    @PrimaryGeneratedColumn("uuid")
    uuid: string;

    @Column({type: 'date'})
    birth_date: Date;

    @Column({type: 'date'})
    entry_date: Date;
}

我插入 2022-03-15 (YYYY-MM-DD),记录在表中保存为 2022-03-14

需要说明的是,我已经进行了相应的测试来验证插入前日期没有发生变异,最终问题出在mssql依赖上。

调查后我发现存在如下所述的问题:

TypeORM 插入日期不正确

将类型更改为 datetimeoffset

对我有用,如下所述:
@Entity("employees")
export class Employees extends BaseEntity {
    @PrimaryGeneratedColumn("uuid")
    uuid: string;

    @Column({type: 'datetimeoffset'})
    birth_date: Date;

    @Column({type: 'datetimeoffset'})
    entry_date: Date;
}

我所掌握的细节是,在执行相应的迁移时,列的数据类型将更改为datetimeoffset,我认为这不是一个好的做法。 对我来说似乎非常“模糊”的另一个选项是添加从日期中减去的那一天,然后再插入它 但我认为这两种解决方案都不是最优的。

我已经更新了 TypeORM 和 MSSQL 依赖项,但问题仍然存在 我该如何解决呢? 有什么想法吗?

node.js sql-server typeorm
1个回答
0
投票
createBuzonDto.desde = 新日期(createBuzonDto.desde.getUTCFullYear(), createBuzonDto.desde.getUTCMonth(), createBuzonDto.desde.getUTCDate())

© www.soinside.com 2019 - 2024. All rights reserved.