Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knex and MySQL datetime field precision

When using knex in a node application is there any way to set a MySQL datetime column as datetime(3) or datetime(6), or better yet set a default for all datetime columns including .timestamps?

Right now it looks like I'm getting the milliseconds truncated when I insert into the DB and then retrieve.

Thanks!

like image 993
Tatsu Avatar asked Mar 24 '16 06:03

Tatsu


1 Answers

After asking about this exact situation on the knex gitter channel, I was pointed to the .specificType() function for handling this specific situation. So (in mysql, for example):

table.specificType('createdAt', 'DATETIME(6)');

would create a DATETIME column named createdAt with fractional seconds of up to 6 precision. I will update this answer to confirm once I have had a chance to test with my local code.

UPDATE: This does indeed work as expected.

like image 86
Paul Griffin Avatar answered Oct 30 '22 10:10

Paul Griffin