Search code examples
typescriptpostgresqltypeorm

Missing timestamp when using TypeORM and postgresql


This is my user entity:

@Entity({ name: 'users' })
export class User extends BaseEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'email' })
  email: string;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at', nullable: true })
  updatedAt: Date;
}

When I select column from table user with code:

const query = User.createQueryBuilder('users');

query.select([
   'users.id',
   'users.email',
   'users.createdAt',
   'users.updatedAt',
]);
return query.find();    

The result contains both create and update timestamp. But with code below, timestamp is disappear:

query.select([
   'users.id',
   'users.email',
   'users.created_at',
   'users.updated_at',
]);
return query.find();  

And when I set logging true in typeORM options, I run 2 queries and both results have created and updated time. Thank for your attention.


Solution

  • The name option you pass to @CreateDateColumn and @UpdateDateColumn is just the way to change the name of the column in postgresql database for your preference, they can't be used to refer to their values.

    If you not use the option name (created_at and updated_at) in two of those fields, by default the name of the column in database is the same as the name of property that you defined (here is createdAt and updatedAt).