Option to overwrite default "created_at" value

System Information
  • Strapi Version: 3.1.7
  • Operating System: Windows 10
  • Database: sqlite
  • Node Version: v10.16.3
  • NPM Version: 6.11.3
  • Yarn Version: 1.9.2

Hey everyone!
I’m just wondering if there’s a way to overwrite the default “created_at” value that strapi adds to all collection types. There are some collection types where I would like to overwrite it and put a different date.
So far, a normal post request, for example:

{
“user”: 1,
“project_name”: 1,
“type”: “internal”,
“created_at”: “2021-01-07T23:15:56.567Z”
}

would create a record, but it will totally ignore the “created_at” value. Is there a way to overwrite this behaviour or is the only solution adding a custom field?

Many thanks!

Short answer to this is “no”*

Long answer is mostly no but with a work-around, the main reason for the no is we simply set the type-value on the DB schema (for SQL databases) or just set the type-value (without validation) on MongoDB

The query apis basically sanitize this out if you actually tried to modify it as the databases themselves set the values, not Strapi.

The workaround would be to disable time stamps on the model settings (can’t do this through the UI, have to edit the model settings manually) and then add your own datetime field that you can control.

1 Like

Got it!
Thank you very much, appreciated! :slight_smile:

No problem :slight_smile:

Hey @Stefan_Ilic,

I’m not sure on how often you would like to do this, but couldn’t just modify it directly in your database suit your needs?

@DMehaffy is this even possible?

Yes either via bookshelf or even RAW Knex queries, it’s just not generally recommended. If it’s a very hard requirement you could though. (Or if you are a mongoose user, it’s also possible there too)

@MattieBelt, I created some kind of a “ticketing” system, but later on I figured out that I need to allow users to set a different date (so changing data manually in strapi itself wouldn’t be an option).
But, as DMehaffy explained, it would be complicated, and I didn’t really want to mess with the strapi core, so I just added a new value to the collection.
Cheers!

1 Like