March 02, 2021 • 1 min read
CODE BITESQuick guide on adding get_random_uuid()
to Sequelize while using Postgresql
Migration to add extension
queryInterface.sequelize.query("CREATE EXTENSION IF NOT EXISTS pgcrypto;")
Model definition
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