Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

383
Visualizações
Current date in TypeORM and PostgreSQL

What's the best way to have a field for a date in PostgreSQL and TypeORM?

I'm relatively new in PostgreSQL (in general SQL database), but in MongoDB (with Mongoose) I usually have a field like this:

export class User {
  @Prop({ default: Date.now() })
  createdAt: Date;
}

What would be the equivalent to this in TypeORM and PostgreSQL? Because, if I use this approach, PostgreSQL throws me an error.

QueryFailedError: date/time field value out of range: "1634416004545"

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use the @CreateDateColumn decorator to archieve what you want:

export class User {
  @CreateDateColumn()
  createdAt: Date;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I know it's an old one, but it's one I stumbled on today and with no approved answer, so I thought it could be of help to someone like me in the future.

Basically, the problem comes from using Date.now() instead of new Date(). My working example looks like this

  @Column({ type: "timestamptz", default: () => "CURRENT_TIMESTAMP" })
  createdDate: Date;

If the value is not provided, it will assign the current value. Note here that if you update a row and the new value misses the createdDate field, the old one will be overwritten.

Then if you want to set it manually, you simply use new Date() like this

const created = { ..., createdDate: new Date(), ...}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda