You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support for different property and column names of timestamps #2209
We now can configure property and column names of models to be different. This is not yet possible with the generated timestamps.
As a workaround, I can add createdAt, etc… to the regular attributes. But then, I also have to set timestamps to false to prevent these attributes from being twice in the queries. This way, I'd lose those nice automatism and I'd have to do this for each model. Wouldn't it be better to have a backwards compatible option for this?
This sets created_at as the property name AND column name (like it is now):
Because when you use associations (belongsTo, hasOne, hasMany, ...) reference fields are named using underscores in tables and in getters/setters, so same problem.
Reacted by Felix Becker, elixiao, Wédney Yuri, areeb, rob phillips, Harold Waterkeyn, Pedro de Assis, Arian Amiri and Charlie Deuter
@Freezystem - yes exactly - I would like to be able to use camelCase in my code but lower_scored in the database. This feature would be huge for me right now and I assume many others. I've been doing the camelCode -> lower_scored_db convention since I was a weee lad, and it was something I picked up from someone else back in the day.
For the impatient, here's a quick & dirty workaround:
sequelize.addHook('afterDefine',(model)=>{if(model.rawAttributes['createdDatetimeUtc']){model.rawAttributes.createdDatetimeUtc.field='created_datetime_utc';}if(model.rawAttributes['modifiedDatetimeUtc']){model.rawAttributes.modifiedDatetimeUtc.field='modified_datetime_utc';}});sequelize.define('myModel',{// normal attributes go here// no need to add the timestamp columns (they'll be overwritten)},{timestamps: true,createdAt: 'createdDatetimeUtc',updatedAt: 'modifiedDatetimeUtc',deletedAt: false});
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
Activity
yonjah commentedon Oct 24, 2014
👍
eventhough commentedon Feb 4, 2015
+1
ayoola-solomon commentedon Sep 28, 2015
Freezystem commentedon Apr 26, 2016
Or maybe just an option to convert any field automatically like:
Because when you use associations (belongsTo, hasOne, hasMany, ...) reference fields are named using underscores in tables and in getters/setters, so same problem.
DesignByOnyx commentedon Apr 29, 2016
@Freezystem - yes exactly - I would like to be able to use camelCase in my code but lower_scored in the database. This feature would be huge for me right now and I assume many others. I've been doing the camelCode -> lower_scored_db convention since I was a weee lad, and it was something I picked up from someone else back in the day.
elixiao commentedon Sep 2, 2016
+1
felixfbecker commentedon Sep 2, 2016
I really like this idea.
teogeos commentedon Oct 26, 2016
It is now possible to do this for foreign keys in associations
foreignKey: { name: 'userId', field: 'user_id' }Is there any plan to allow doing this with timestamps?
felixfbecker commentedon Oct 26, 2016
If someone wants to implement it
rafael-pinho commentedon Mar 29, 2017
Is anyone implementing it? If not, I will start to implementing it.
sushantdhiman commentedon Sep 24, 2017
@rafael-pinho You should send a PR
schmod commentedon May 15, 2018
For the impatient, here's a quick & dirty workaround:
stale commentedon Jul 23, 2019
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
1valdis commentedon Feb 25, 2020
No way to achieve this without workarounds yet?