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.

How to set default value for datetime in nest ORM column for mysql

See original GitHub issue

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

Does anyone know how to set default value of datetime type for nest ORM mysql I tried

@Column({ type: 'datetime', default: () => new Date()}) .
@Column({ type: 'datetime', default: () => '2019-06-29'})
@Column({ type: 'datetime', default: 'CURRENT_TIMESTAMP'})
@Column({ type: 'datetime', default: 'now()'})

All failed.

I will be very appreciated for any help.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:14

github_iconTop GitHub Comments

6reactions
robertmaincommented, Feb 18, 2020

Yeah, you probably don’t want to do that, because that default value is evaluated only when the migration is run. So what you’re effectively doing is snapshotting the current time when the migration is being run, then telling MySQL to always use that value as the default value. So what you’re effectively running is this:

@Column({
  nullable: false,
  default: () => new Date(2020, 1, 18, 17, 00), 
  type: 'timestamp',
})

Which will produce something like the following SQL:

ADD COLUMN `mycolumnname` TIMESTAMP NOT NULL DEFAULT '2020-02-18 17:00:00-05:00'

Meaning that every time you insert data into your table, your column will ALWAYS have the default value of '2020-02-18 17:00:00-05:00' irrespective of what the actual current date/time are.

What you probably want to do is have MySQL re-calculate that value every time, so you want to shoot for the following SQL:

ADD COLUMN `mycolumnname` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

I’m guessing at this point because I’ve migrated most of my apps to postgres, but you probably want something like this in your column definition:

@Column({
  nullable: false,
  default: () => 'CURRENT_TIMESTAMP' 
  type: 'timestamp',
})
5reactions
robertmaincommented, Feb 18, 2020

MySQL does…but TypeORM’s integration with MySQL may not. It’s hard to say, but I ended up switching to postgres (for a variety of reasons). I know that “jUSt SwITch tO PosTGrES” isn’t really a solution, but if you can it tends to be a much better developer experience all round 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I create columns with type Date and type DateTime ...
In @Column there is an option called type -> here is where you specify which type of date you want to store for...
Read more >
How to Set Default Value for Datetime Column in MySQL
Sometimes you may need to set default value for datetime column in MySQL. Here are the steps to do it.
Read more >
11.2.6 Automatic Initialization and Updating for TIMESTAMP ...
For any TIMESTAMP or DATETIME column in a table, you can assign the current timestamp as the default value, the auto-update value, or...
Read more >
Entities - typeorm - GitBook
insert: boolean - Indicates if column value is set the first time you insert the object. Default value is true .
Read more >
Data model (Reference) - Prisma
Learn about the concepts for building your data model with Prisma: Models, scalar types, enums, attributes, functions, IDs, default values and more.
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