如何使用 sequelize 播种 uuidv4

How to seed uuidv4 with sequelize

我正在尝试使用 sequelize generate UUID 为 uuid 播种,但出现此错误 Seed file failed with error: sequelize.Utils.generateUUID is not a function TypeError: sequelize.Utils.generateUUID is not a function

我如何播种 UUID?

    return queryInterface.bulkInsert('companies', [{
        id: sequelize.Utils.generateUUID(),
        name: 'Testing',
        updated_at: new Date(),
        created_at: new Date()
    }]);

只需安装 uuid:

npm install uuid

在你的种子文件上:

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

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.bulkInsert('yourTableName', [
      {
        id: uuidv4()
      }],
 {});

因为它包含在 DataTypes 包中,所以按照 Matt 的建议使用 already available 是有意义的。你可以这样使用它:

{
    ...,
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    ...
}

npm install uuid

将其导入您的种子文件

const { v4: uuidv4 } = require('uuid');

return queryInterface.bulkInsert('companies', [{
    id: uuidv4(),
    name: 'Testing',
    updated_at: new Date(),
    created_at: new Date()
}]);

See more on the uuid documentation