@CreateDateColumn() do not set new Date automatically

See original GitHub issue

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [X] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[X] latest [ ] @next [X] 0.2.22

Steps to reproduce or a small repository showing the problem: All my tables defined in Postgres database have the auditing columns “created_at” , “updated_at” and “version”. The created_at and version are by default, set as “NOT NULL”.

For some reason, the ORM by default is not setting a new date when inserting a new record, even with the @CreateDateColumn() decorator declared. However, everything works fine regards to the version property with the @VersionColumn() decorator. Both are not null in the Database.

I am using a workaround with @BeforeInsert() to set a new Date. However, it is not elegant at all.

My column declaration:

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

  @UpdateDateColumn()
  @Column({ name: 'updated_at' })
  updatedAt!: Date;

  @VersionColumn()
  @Column()
  private version!: number;

Down below, the log the shows evidence that the ORM is not setting the date automatically:

query: START TRANSACTION
query: INSERT INTO "contacts"("type", "name", "email", "website", "phone1", "phone1_ext", "phone2", "phone2_ext", "address", "address_compl", "city", "province", "postal_code", "country", "created_at", "updated_at", "version") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, DEFAULT, DEFAULT, DEFAULT) RETURNING "id" -- PARAMETERS: ["P","Leonardo","","","6479154312","","34535435345","a","80, Industry St.","Suite 100","Toronto","ON","M9V5E3","CA"]
query failed: INSERT INTO "contacts"("type", "name", "email", "website", "phone1", "phone1_ext", "phone2", "phone2_ext", "address", "address_compl", "city", "province", "postal_code", "country", "created_at", "updated_at", "version") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, DEFAULT, DEFAULT, DEFAULT) RETURNING "id" -- PARAMETERS: ["P","Leonardo","","","6479154312","","34535435345","a","80, Industry St.","Suite 100","Toronto","ON","M9V5E3","CA"]
error: error: null value in column "created_at" violates not-null constraint
    at Connection.parseE (/home/wallup1/dev/wallupcms/node_modules/pg/lib/connection.js:604:13)
    at Connection.parseMessage (/home/wallup1/dev/wallupcms/node_modules/pg/lib/connection.js:403:19)
    at Socket.<anonymous> (/home/wallup1/dev/wallupcms/node_modules/pg/lib/connection.js:123:22)
    at Socket.emit (events.js:210:5)
    at Socket.EventEmitter.emit (domain.js:476:20)
    at addChunk (_stream_readable.js:308:12)
    at readableAddChunk (_stream_readable.js:289:11)
    at Socket.Readable.push (_stream_readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:182:23) {
  name: 'error',
  length: 301,
  severity: 'ERROR',
  code: '23502',
  detail: 'Failing row contains (1, P, Leonardo, , , 6479154312, , 34535435345, a, 80, Industry St., Suite 100, Toronto, ON, M9V5E3, CA, null, null, null).',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: 'public',
  table: 'contacts',
  column: 'created_at',
  dataType: undefined,
  constraint: undefined,
  file: 'execMain.c',
  line: '2042',
  routine: 'ExecConstraints'
}

More details:

[2020-01-17T19:11:00.802] [DEBUG] ContactModel.ts QueryFailedError: null value in column "created_at" violates not-null constraint
    at new QueryFailedError (/home/wallup1/dev/wallupcms/src/error/QueryFailedError.ts:9:9)
    at Query.<anonymous> (/home/wallup1/dev/wallupcms/src/driver/postgres/PostgresQueryRunner.ts:178:30)
    at Query.handleError (/home/wallup1/dev/wallupcms/node_modules/pg/lib/query.js:145:17)
    at Connection.connectedErrorMessageHandler (/home/wallup1/dev/wallupcms/node_modules/pg/lib/client.js:214:17)
    at Connection.emit (events.js:210:5)
    at Connection.EventEmitter.emit (domain.js:476:20)
    at Socket.<anonymous> (/home/wallup1/dev/wallupcms/node_modules/pg/lib/connection.js:128:12)
    at Socket.emit (events.js:210:5)
    at Socket.EventEmitter.emit (domain.js:476:20)
    at addChunk (_stream_readable.js:308:12) {
  message: 'null value in column "created_at" violates not-null constraint',
  name: 'QueryFailedError',
  length: 300,
  severity: 'ERROR',
  code: '23502',
  detail: 'Failing row contains (2, P, Leonardo, , , 6479154312, , 34535435345, a, 876, Maine St., Suite 1456, Toronto, ON, M8T7Y9, CA, null, null, null).',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: 'public',
  table: 'contacts',
  column: 'created_at',
  dataType: undefined,
  constraint: undefined,
  file: 'execMain.c',
  line: '2042',
  routine: 'ExecConstraints',
  query: 'INSERT INTO "contacts"("type", "name", "email", "website", "phone1", "phone1_ext", "phone2", "phone2_ext", "address", "address_compl", "city", "province", "postal_code", "country", "created_at", "updated_at", "version") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, DEFAULT, DEFAULT, DEFAULT) RETURNING "id"',
  parameters: [
    'P',              'Leonardo',
    '',               '',
    '6479154312',     '',
    '34535435345',    'a',
    '876, Maine St.', 'Suite 1456',
    'Toronto',        'ON',
    'M8T7Y9',         'CA'
  ]
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:10
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
geunyoungnocommented, Feb 20, 2020

same issue in mysql UpdateDateColumn works well, but CreateDateColumn doesn’t insert date.

4reactions
artoodeetocommented, Apr 2, 2020

@leonardootoni @geunyoungno yow my dudes. I just have a workaround. so you can set your migration something like this.

createdAt DATETIME      DEFAULT CURRENT_TIMESTAMP
OR
{
   name: 'createdAt',
   type: 'DATETIME',
   isNullable: true,
   default: 'CURRENT_TIMESTAMP'
},
Read more comments on GitHub >

github_iconTop Results From Across the Web

CreateDateColumn/UpdateDateColumn save an incorrect ...
The issue is, that the time saved in the columns CreateDateColumn/UpdateDateColumn , saves a different hour compare with the time on my local ......
Read more >
Decorator reference - typeorm - GitBook
Special column that is automatically set to the entity's update time each time you call save from entity manager or repository. You don't...
Read more >
Set and use date tables in Power BI Desktop
Power BI Desktop works behind the scenes to automatically identify columns that represent dates, and then creates date hierarchies and other ...
Read more >
TypeORM - Quick Guide
@CreateDateColumn − It is a special column to set entity's insertion date automatically. @UpdateDateColumn − It is used to set the entity's update...
Read more >
PostgreSQL and typeorm - Advanced Querying
There are date only and time only database types in postgres but I ... You define an array in the typeorm column, but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found