'sequelize migration throws ERROR: function uuid_generate_v4() does not exist

I enabled uuid extension with CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

Then ran same query generated by sequelize for migration and it worked,

create table if not exists "MY_SCHEMA"."Users" ("id" UUID not null default uuid_generate_v4() ,
"phone" VARCHAR(10) not null,
"password" VARCHAR(50) not null,
"isDeleted" BOOLEAN default false,
"role" "MY_SCHEMA"."enum_Users_role" default 'Student',
"createdAt" TIMESTAMP with TIME zone not null,
"updatedAt" TIMESTAMP with TIME zone not null,
primary key ("id"));

But, sequelize throws below error,

ERROR: function uuid_generate_v4() does not exist

Here is the migration file,

const SCHEMA = 'MY_SCHEMA';
const TABLE_WITH_SCHEMA = { tableName: 'Users', schema: SCHEMA };

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable(TABLE_WITH_SCHEMA, {
      id: {
        allowNull: false,
        primaryKey: true,
        defaultValue: Sequelize.literal('uuid_generate_v4()'),
        type: Sequelize.UUID
      },
      phone: {
        type: Sequelize.STRING(10),
        allowNull: false
      },
      password: {
        type: Sequelize.STRING(50),
        allowNull: false
      },
      isDeleted: {
        type: Sequelize.BOOLEAN,
        defaultValue: false
      },
      role: {
        type: Sequelize.ENUM('Admin', 'Teacher', 'Student'),
        defaultValue: 'Student'
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
    await queryInterface.addIndex(TABLE_WITH_SCHEMA, ['phone'], {
      indicesType: 'UNIQUE'
    });
  },
  async down(queryInterface) {
    await queryInterface.dropTable(TABLE_WITH_SCHEMA);
  }
};


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source