0

It's the third day I'm trying to accomplish this simple task using sequelize but I'm having a really hard time doing it.
All I want is to create a table with two fields:

  • id (type: int)
  • publicId (type: uuid)

The id field is the primary key, auto increments etc.. The publicId field should be of the type UUID and it's values should be automatically generated, by the "defaultValue" attribute.
I've tried all sorts of combinations in the creation of the model or the migration file when creating the table. Also it is not a problem with sync because each time I remove my db container, spawn a new one and migrate all again.

I'm using sequelize-cli v5.5.1, but I tried v6.2 as well.

The code:
(migration file)

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('orders', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      publicId: {
        type: Sequelize.UUID,
        allowNull: false,
        unique: true,
        defaultValue: Sequelize.UUIDV4,
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('orders');
  }
};

model file:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const order = sequelize.define('order', {
  }, {});

  order.associate = function(models) {
    // associations can be defined here
  };
  return order;
};

The migration is ran successfully, but only the 'id' field is set with default values. The publicId field just has the non-null constraint, and the default values are not created.
I've tried many other approaches, like setting a function as defaultValue that returns a uuid, already tried Sequelize.fn('uuid_generate_v4'), and others.. defaultValues field just doesn't seem to work.

Teodoro
  • 1,194
  • 8
  • 22

1 Answers1

0

Try adding the new columns to the model file as well, and designating the attribute options.

As an aside -- I always turn off strict mode because it's given me strange issues.

Horus
  • 11
  • 3