Skip to content

Support for different property and column names of timestamps #2209

Description

@VividVisions

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):

new Sequelize(foo, bar, baz, {
  define: {
    timestamps: true,
    createdAt: 'created_at'
  }
});

This would use createdAt as property and created_at as column name (analogous to the field option in column definitions).

new Sequelize(foo, bar, baz, {
  define: {
    timestamps: true,
    createdAt: {
        name: 'createdAt',
        field: 'created_at'
    }
  }
});

Activity

  1. yonjah commented on Oct 24, 2014

    @yonjah
    Contributor

    👍

  2. eventhough commented on Feb 4, 2015

    @eventhough

    +1

  3. ayoola-solomon commented on Sep 28, 2015

    @ayoola-solomon

    image

  4. Freezystem commented on Apr 26, 2016

    @Freezystem

    Or maybe just an option to convert any field automatically like:

    new Sequelize(foo, bar, baz, {
      define: {
        timestamps: true,
        table : {
          underscored : true,
        }
        script : {
          underscored : false
        }
      }
    });

    Because when you use associations (belongsTo, hasOne, hasMany, ...) reference fields are named using underscores in tables and in getters/setters, so same problem.

  5. DesignByOnyx commented on Apr 29, 2016

    @DesignByOnyx

    @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.

  6. elixiao commented on Sep 2, 2016

    @elixiao

    +1

  7. felixfbecker commented on Sep 2, 2016

    @felixfbecker
    Contributor

    Or maybe just an option to convert any field automatically like:

    new Sequelize(foo, bar, baz, {
      define: {
        timestamps: true,
        table : {
          underscored : true,
        }
        script : {
          underscored : false
        }
      }
    });

    I really like this idea.

  8. teogeos commented on Oct 26, 2016

    @teogeos

    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?

  9. felixfbecker commented on Oct 26, 2016

    @felixfbecker
    Contributor

    If someone wants to implement it

  10. rafael-pinho commented on Mar 29, 2017

    @rafael-pinho

    Is anyone implementing it? If not, I will start to implementing it.

  11. sushantdhiman commented on Sep 24, 2017

    @sushantdhiman
    Contributor

    @rafael-pinho You should send a PR

  12. schmod commented on May 15, 2018

    @schmod
    Contributor

    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
    });
  13. stale commented on Jul 23, 2019

    @stale

    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 🙂

  14. 1valdis commented on Feb 25, 2020

    @1valdis

    No way to achieve this without workarounds yet?

  15. added theissue type on Mar 9, 2025
  16. removed
    type: feature
    on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions