How to set default value for datetime in nest ORM column for mysql
See original GitHub issueIssue 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:
- Created 4 years ago
- Reactions:2
- Comments:14
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:Which will produce something like the following SQL:
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:
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:
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 😃