FG
๐Ÿ’ป Software๐Ÿ—„๏ธ Databases

Date column returns a date string instead of a date object

Fresh3 days ago
Mar 14, 20260 views
Confidence Score66%
66%

Problem

Issue type: [ ] question [x] bug report [ ] feature request [ ] documentation issue Database system/driver: [ ] `cordova` [ ] `mongodb` [ ] `mssql` [ ] `mysql` / `mariadb` [ ] `oracle` [x] `postgres` [ ] `sqlite` [ ] `sqljs` [ ] `react-native` TypeORM version: [ ] `latest` [ ] `@next` [x] `0.2.6` (or put your version here) If I set a date column with `@CreateDateColumn({type:'Date'})` in an entity. The result contains a date string `{created: "2018-05-18"}` instead of a date object. I think it's a bug because a date time column `@CreateDateColumn()` would return a date object. [code block]`

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Fix Date Column Type in TypeORM Entity

Medium Risk

The issue occurs because the `@CreateDateColumn({type: 'Date'})` decorator is not properly converting the date to a Date object in the PostgreSQL database. Instead, it returns a string representation of the date. This behavior is due to the incorrect type specification in the TypeORM entity definition, which should use 'timestamp' or 'timestamptz' for date-time columns in PostgreSQL.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Update Entity Decorator

    Change the type in the `@CreateDateColumn` decorator from 'Date' to 'timestamp' or 'timestamptz' to ensure that the column is treated as a date-time object in PostgreSQL.

    typescript
    import { Entity, PrimaryGeneratedColumn, CreateDateColumn } from 'typeorm';
    
    @Entity()
    export class YourEntity {
        @PrimaryGeneratedColumn()
        id: number;
    
        @CreateDateColumn({ type: 'timestamp' })
        created: Date;
    }
  2. 2

    Run Database Migration

    After updating the entity, run a migration to apply the changes to the database schema. This ensures that the column type is updated in the PostgreSQL database.

    bash
    typeorm migration:generate -n UpdateYourEntityDateColumn
    typeorm migration:run
  3. 3

    Test the Changes

    Insert a new record into the database and retrieve it to confirm that the 'created' field now returns a Date object instead of a string.

    typescript
    const newEntity = await repository.save(new YourEntity());
    const fetchedEntity = await repository.findOne(newEntity.id);
    console.log(fetchedEntity.created instanceof Date); // Should log true
  4. 4

    Verify Application Behavior

    Check the application behavior to ensure that any functionality relying on the 'created' date field operates correctly with the Date object.

Validation

Confirm that the 'created' field in the retrieved entity is of type Date by using `instanceof Date`. Additionally, verify that any application logic that uses this field behaves as expected.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

typeormormtypescriptquestionby-design