如何用续集来播种uuidv4

时间:2017-09-24 09:00:10

标签: node.js sequelize.js sequelize-cli

我正在尝试使用sequilize generateUUID播种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()
    }]);

3 个答案:

答案 0 :(得分:2)

只需安装uuid:

npm install uuid

并在您的种子文件中:

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

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

答案 1 :(得分:0)

由于它已包含在DataTypes包中,因此可以使用Matt建议的already available。您可以通过以下方式使用它:

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

答案 2 :(得分:0)

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