0

I want to automatically update the CreatedDateTime column whenever I want to insert or update column. But my problem is some tables have no CreatedDateTime column.

My code looks like this:

$beforeInsert(context) {
    this.CreatedDateTime = new Date().toISOString();
}

I tried explicitly specified the json schema like this:

static get jsonSchema() {
    return {
        type: 'object',
        properties: {
            CanteenFreeMealLineID: {
                type: 'integer',
            },
            CanteenFreeMealID: {
                type: 'integer',
            },
            DatacenterID: {
                type: 'integer',
            },
        },
    };
}

but it still forces to update to column CreatedDateTime

Ryan Garde
  • 742
  • 1
  • 7
  • 20
  • 1
    Can you alter the tables and let mysql handle this by setting the default value to current_timestamp and on update current_timestamp? [Documentation](https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html) – jkoch Mar 17 '22 at 01:46
  • *my problem is some tables have no CreatedDateTime column.* This is not a problem. `ALTER TABLE ADD COLUMN` solves it easily. – Akina Mar 17 '22 at 04:30
  • @jkoch that will be our last resort. Its hard in our current setup because we are using mssql so the `UpdateDateTime` column is a big problem since we have to use triggers – Ryan Garde Mar 18 '22 at 03:47
  • @Akina some columns should not have `CreatedDateTime` since it does make much sense with pivot tables for example and also some of our tables got `CreatedDate` / `UpdateDate` and not `CreatedDateTime` / `UpdatedDateTime` – Ryan Garde Mar 18 '22 at 03:49
  • *some columns should not have CreatedDateTime since* columns??? *some of our tables got CreatedDate / UpdateDate and not CreatedDateTime / UpdatedDateTime* This means that you use explicit setting instead of autoutilizing. Why? It's not a problem to extract the date part from datetime value. – Akina Mar 18 '22 at 04:12

0 Answers0