question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set default value to NOW in the migration doesn't work

See original GitHub issue

I have created a migration file:

'use strict';

module.exports = {
  up: function(queryInterface, Sequelize) {
    return queryInterface.createTable('products', {
      id: {
        type: Sequelize.STRING,
        primaryKey: true
      },
      name: {
        type: Sequelize.STRING,
        allowNull: false
      },
      ean: {
        type: Sequelize.STRING,
        allowNull: false
      },
      categoryName: {
        type: Sequelize.STRING,
        allowNull: false,
        field: 'category_name'
      },
      price: {
        type: Sequelize.STRING,
      },
      photo: {
        type: Sequelize.STRING,
      },
      fullUrl: {
        type: Sequelize.STRING,
        field: 'full_url'
      },
      createdAt: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW,
        allowNull: false,
        field: 'created_at'
      },
      updatedAt: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW,
        allowNull: false,
        field: 'updated_at'
      }
    }).then(function() {
      return queryInterface.addIndex('products', ['ean']);
    });
  },

  down: function(queryInterface, Sequelize) {
    return queryInterface.dropTable('products');
  }
};

the database I’m using is postgressql. when I create the table in this value no default value is set for the column createdAt and updatedAt. is it a bug?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:5
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

24reactions
Tazercommented, Jun 2, 2017

Hehe no problem, I Solved it doing defaultValue: Sequelize.fn('now'),

Thanks anyways 👍

6reactions
BartusZakcommented, Jul 23, 2019

Sequelize.NOW = blank Default Value Sequelize.fn('now') = CURRENT_TIMESTAMP

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel migration default value - Stack Overflow
The reason why your default value doesnt't work is because the migration file sets up the default value in your database (MySQL or ......
Read more >
Set default value to a date field (in migration) - Laracasts
As the title says I'd like to set a default value for a date field inside the migration. I can't change the date...
Read more >
How to solve the "doesn't have a default value" error in Laravel
You can edit the migration file and set the dob field to nullable , like so: public function up() {
Read more >
Avoid SET/DROP DEFAULT unless a field changes from null ...
You said that "Django uses database defaults to set values on existing rows in a table", but if I migrate my table adding...
Read more >
Generated Values - EF Core - Microsoft Learn
You can configure a default value on a property: ... EF Core providers usually don't set up value generation automatically for date/time ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found