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.

Field createdAt doesn't have a default value Issue

See original GitHub issue

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

I define my User.ts createdAt field in the following way

  @CreateDateColumn({ name: 'createdAt', type: 'datetime' })
  createdAt: Date

If I try to create a new user via the following code

  ...
  sampleUser.createdAt = new Date()
  ...

  await getRepository(User).save(sampleUser)

I get this error:

UnhandledPromiseRejectionWarning: QueryFailedError: ER_NO_DEFAULT_FOR_FIELD: Field 'createdAt' doesn't have a default value

I went into node_modules typeorm code and console logged the query and the params:

[ 'test@test.com',
  'John Doe',
  'hashedpass',
  'salt',
  'test',
  1,
  'admin' ] /// the createdAt field is missing?

Here is the definition of the createdAt property in my sql table:

`createdAt` DATETIME NOT NULL,

The query looks good, but the field createdAt is missing from the params.

  1. The errors should log more details, like the full query and the full params. Some errors are so general that I have no idea where to start debugging.
  2. I’m working on an existing database and I don’t want to run sync, so my table doesn’t have a default value for the createdAt field. Still, I should be able to set my own createdAt field in the code. I fail to understand why something so basic, simple and so straightforward fails?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
AlexMessercommented, Mar 30, 2021

I’m working on an existing database and I don’t want to run sync, so my table doesn’t have a default value for the createdAt field. Still, I should be able to set my own createdAt field in the code

@CreateDateColumn uses database level default to generate a new timestamp. So, you can’t use this decorator without any changes to the database schema.

0reactions
sang-w0ocommented, Feb 18, 2021

Without using the “skipping” way shown in this issue’s title, the same issue is also happening with MySQL and MariaDB also.

It also happens with @UpdateDateColumn decorator.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Field 'createdAt' doesn't have a default value" after ... - GitHub
The problem is that the cli updates the model but doesn't keep track of the added columns and thus umzug doesn't know of...
Read more >
ERROR 1364: Field 'createdAt' doesn't have a default value
sequelize createdAt. updatedAt defaultValue 설정하기.
Read more >
Field 'created_date' doesn't have a default value
I was trying to add a created_date field but get error while inserting sql through data.sql. However, I got the error 'Field 'created_date' ......
Read more >
Field 'updated_at' doesn't have a default value error on server
Right now I'm trying to save a product but I'm getting this error General error: 1364 Field 'updated_at' doesn't have a default value...
Read more >
Broken migration due to Field 'created_at' doesn't have a ...
The problem is caused by the field created_at having no default value. This results in a database migration failure.
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