Mo

BlogStartup ideasTwitter

Sequelize with Postgresql UUID

CODE BITES

Quick guide on adding get_random_uuid() to Sequelize while using Postgresql

Migration to add extension

migration.js
queryInterface.sequelize.query("CREATE EXTENSION IF NOT EXISTS pgcrypto;")

Model definition

user.js
const User = sequelize.define('User', {
  id: { type: DataType.UUID, allowNull: false, primaryKey: true, defaultValue: Sequelize.literal('gen_random_uuid()') },

  firstName: {
    type: DataTypes.STRING,
    allowNull: false
  },
  lastName: {
    type: DataTypes.STRING
  }
});


If you enjoyed this post, feel free to follow me on Twitter or email where you can stay up to date on upcoming content and life updates