Tatsu
Tatsu

Reputation: 143

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!

Upvotes: 6

Views: 2427

Answers (1)

Paul Griffin
Paul Griffin

Reputation: 2436

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.

Upvotes: 7

Related Questions