question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

postgres fails to use @CreatedDateColumn and @UpdateDateColumn

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 [ ] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[ ] latest [ ] @next [x] 0.2.1 (or put your version here)

Steps to reproduce or a small repository showing the problem:

import {
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Entity,
  PrimaryGeneratedColumn
} from 'typeorm';
import {
  IsNotEmpty,
  IsUUID
} from 'class-validator';

@Entity('applications')
export class Application {
  @PrimaryGeneratedColumn()
  readonly id!: number;

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

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp' })
  updatedAt: Date;
}
saving entity failed { QueryFailedError: null value in column "created_at" violates not-null constraint
    at new QueryFailedError (/Users/et/code/NeatCapitalOrg/finicity-service/src/error/QueryFailedError.ts:7:9)
    at Query.<anonymous> (/Users/et/code/NeatCapitalOrg/finicity-service/src/driver/postgres/PostgresQueryRunner.ts:170:26)
    at Query.handleError (/Users/et/code/NeatCapitalOrg/finicity-service/node_modules/pg/lib/query.js:143:17)
    at Connection.connectedErrorHandler (/Users/et/code/NeatCapitalOrg/finicity-service/node_modules/pg/lib/client.js:132:26)
    at emitOne (events.js:115:13)
    at Connection.emit (events.js:210:7)
    at Socket.<anonymous> (/Users/et/code/NeatCapitalOrg/finicity-service/node_modules/pg/lib/connection.js:117:12)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:266:12)
  message: 'null value in column "created_at" violates not-null constraint',
  name: 'QueryFailedError',
  length: 237,
  severity: 'ERROR',
  code: '23502',
  detail: 'Failing row contains (65, b03e832d-3862-43cb-8e6f-4984378a31a9, null, null).',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: 'public',
  table: 'applications',
  column: 'created_at',
  dataType: undefined,
  constraint: undefined,
  file: 'execMain.c',
  line: '2008',
  routine: 'ExecConstraints',
  query: 'INSERT INTO "applications"("nc_uuid", "created_at", "updated_at") VALUES ($1, DEFAULT, DEFAULT) RETURNING "id", "created_at", "updated_at"',
  parameters: [ 'b03e832d-3862-43cb-8e6f-4984378a31a9' ] }
Could not run seed QueryFailedError: null value in column "created_at" violates not-null constraint

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
etcommented, Apr 25, 2018

Thanks for checking that @rclayton-the-terrible. I confirmed the same results. Since this is a breaking change, it should be emphasized that people using these decorators need to create a migration to properly set the default to NOW().

import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddDefaultToTimestamps1524669290077 implements MigrationInterface {

  async up(queryRunner: QueryRunner): Promise<any> {
    await queryRunner.query('ALTER TABLE applications ALTER COLUMN created_at SET DEFAULT NOW()');
    await queryRunner.query('ALTER TABLE applications ALTER COLUMN updated_at SET DEFAULT NOW()');
  }

  async down(queryRunner: QueryRunner): Promise<any> {
    await queryRunner.query('ALTER TABLE applications ALTER COLUMN created_at DROP DEFAULT;');
    await queryRunner.query('ALTER TABLE applications ALTER COLUMN created_at DROP DEFAULT;');
  }
}
3reactions
zarelitcommented, Feb 28, 2022

I just spent something like 6 hours to figure out that my problem was related to an issue closed in 2018. Is there an issue to track the documentation update?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I create columns with type Date and ...
For example (using PostgreSQL): @Column({ type: 'date' }) date_only: ... Last Updated Date, available via @UpdateDateColumn() decorator.
Read more >
Documentation: 15: CREATE VIEW
Use the DROP VIEW statement to drop views. Be careful that the names and types of the view's columns will be assigned the...
Read more >
Documentation: 15: 19.3. Starting the Database Server
If that variable is not provided either, it will fail. Normally it is better to start postgres in the background. For this, use...
Read more >
Documentation: 15: ALTER TABLE
The new default value will only apply in subsequent INSERT or UPDATE commands; it does not cause rows already in the table to...
Read more >
Documentation: 15: postgres
Note that this will happen even when within a string literal or comment. But if you use the -j command line switch, a...
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