@CreateDateColumn() do not set new Date automatically
See original GitHub issueIssue 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:
- Created 4 years ago
- Reactions:10
- Comments:10 (5 by maintainers)
Top Related StackOverflow Question
same issue in mysql UpdateDateColumn works well, but CreateDateColumn doesn’t insert date.
@leonardootoni @geunyoungno yow my dudes. I just have a workaround. so you can set your migration something like this.