Database Manager - editing timestamp fields

Am I doing something wrong with this field addition

I would like a default value for current_timestamp and if possible an ON Update Current_timestamp too.

Try with

knex.fn.now()
1 Like

I think we have to improve setting the defaults to the Knex functions indeed as currently those are just passed as strings

2 Likes

@george, any updates on this?

I’m running into the same issue. I want every table to have a “CreatedDate” and “UpdatedDate” field. Both should default to current date/time when a record is created. Unfortunately, I’m unable to get past the error – even if I correct the knex file by removing the single quotes, save it, then try to push the changes, Wappler is modifying the file leading to the same error.

Modifying to this:

Becomes this after trying to push:

image

1 Like

Use timestamps.

timestampstable.timestamps([useTimestamps], [defaultToNow])

Adds created_at and updated_at columns on the database, setting each to datetime types. When true is passed as the first argument a timestamp type is used instead. Both colums default to being not null and using the current timestamp when true is passed as the second argument. Note that on MySQL the .timestamps() only have seconds precision, to get better precision use the .datetime or .timestamp methods directly with precision.

1 Like

@JonL that would work great for what I’m looking to do and save some time (you could even have a way to specify this for all tables), but I think Wappler could also figure out a way to allow for other methods in the default field for other scenarios.

1 Like

I agree 100%. I was just referring to your use case.

I also opened a FR for this:

1 Like

@JonL Thanks for this info (whatever I’m working on - you have recently posted a solution :grinning:)

However the ‘updated_on’ column doesn’t seem to be updating. Any idea what could be going wrong? Please see this video where I update a record but the ‘updated at’ is still stuck on 2020-8-17

Glad you are finding my posts helpful :slight_smile:

So the knex default types in the migration files only handle schema creation. Both columns will be created automatically for you and defaulted to a now timestamp on creation but nothing will happen on update.

To update updated_at with every change in that specific table you need to create a trigger.

You could try to add a raw statement to your migration file.

If you are using MYSQL it is easier as you can use ON UPDATE CURRENT_TIMESTAMP