有些时候,为了生成唯一、不暴露数据库内部细节的 ID 时,可以使用 UUID 作为模型的主键。
例如有一个订单表,里面会有一个订单号。但如果使用自增 ID 作为订单号,容易暴露订单量、企业销售速度等敏感商业信息。则可以考虑使用 UUID 作为订单号。
创建模型与迁移
sequelize model:generate --name Order --attributes userId:integer,tradeNo:string,subject:string,totalAmount:decimal,status:boolean,paymentMethod:tinyint,membershipMonths:integer,paidAt:date
修改迁移
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
...