Search code examples
migrationsequelize.jsuuid

Sequelize migration converting Sequelize.UUID primary key field to integer autoincrement in MYSQL


I am using sequelize CLI to generate and run db migrations. The issue I am having is the id field set to data type Sequelize.UUID appearing as an autoincrement integer in mysql. Here is my user model and migration:

User Model

    'use strict';
    module.exports = (sequelize, DataTypes) => {
      const User = sequelize.define('User', {
        UserName: {type:DataTypes.STRING,unique:true,allowNull:false},
        FirstName:{type: DataTypes.STRING,allowNull:true},
        LastName: {type:DataTypes.STRING,allowNull:true},
        Email: {type:DataTypes.STRING,allowNull:false,unique:true,validate: { isEmail: {msg: "Invalid Email"} }},
        Password: {type:DataTypes.STRING,allowNull:false},
        Avatar: {type:DataTypes.STRING,allowNull:true},

      }, {});
      User.associate = function(models) {

        User.hasMany(models.RoleUser,
            {
                foreignKey:'UserId',
                as:'userroles',
                sourceKey:'id'
            }),

        User.belongsTo(models.Country,
            {
                foreignKey:'CountryId',
                targetKey:'id'
            }),

            User.belongsToMany(models.Role,
                {
            through: 'RoleUser',
            foreignkey:'UserId'

        })


     };
      return User;
    };

**User Migration file:**

'use strict';
const uuidv4 = require('uuid/v4');

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.UUID
        defaultValue:uuidv4()
      },
      UserName: {
        type: Sequelize.STRING
      },
      FirstName: {
        type: Sequelize.STRING
      },
      LastName: {
        type: Sequelize.STRING
      },
      Email: {
        type: Sequelize.STRING
      },
      Password: {
        type: Sequelize.STRING
      },
      Avatar: {
        type: Sequelize.STRING
      },

      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Users');
  }
};

AFTER MIGRATION, THE HIS FIELD IS CONVERTED TO INT AUTOINCREMENT IN MYSQL:

id: {
            allowNull: false,
            primaryKey: true,
            type: Sequelize.UUID
            defaultValue:uuidv4()
          },

Any pointer as to why this is happening? Please assist. Even the associations seem not to be formed at all as foreign keys are of type Sequelize.UUID


Solution

  • I fixed the problem by adding the id field on model with primary key property set to true.

     id: {
    type:DataTypes.UUID,
    allowNull:false,
    unique:true,
    primaryKey:true
    },
    

    Its like sequelize will automatically generate id field of type INTEGER AUTOINCREAMENT if the model does not have a field with primary key set to true.