14

I am trying to seed a uuid using sequilize generateUUID but i get this error Seed file failed with error: sequelize.Utils.generateUUID is not a function TypeError: sequelize.Utils.generateUUID is not a function

how do i seed a UUID?

    return queryInterface.bulkInsert('companies', [{
        id: sequelize.Utils.generateUUID(),
        name: 'Testing',
        updated_at: new Date(),
        created_at: new Date()
    }]);
Amir Sasson
  • 10,985
  • 1
  • 13
  • 25
WalksAway
  • 2,769
  • 2
  • 20
  • 42

3 Answers3

13

just install uuid:

npm install uuid

and on your seed file:

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

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.bulkInsert('yourTableName', [
      {
        id: uuidv4()
      }],
 {});
Amir Sasson
  • 10,985
  • 1
  • 13
  • 25
  • 3
    Sequelize does use `uuid` under the hood so the package should already be available. Sequelize doesn't expose the `uuidv4` method in a useful way though. – Matt Nov 15 '19 at 01:57
  • Now getting `DeprecationWarning: Deep requiring like 'const uuidv4 = require('uuid/v4');' is deprecated as of uuid@7.x. Please require the top-level module when using the Node.js CommonJS module or use ECMAScript Modules when bundling for the browser. See https://github.com/uuidjs/uuid#deep-requires-now-deprecated for more information.` And when I switch to the recommended import syntax I get a different error. – dmikester1 Mar 20 '20 at 13:32
6

Since it's included in the DataTypes package, would make sense to use what is already available as Matt suggested. You can use it in this way:

{
    ...,
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    ...
}
Hichame Yessou
  • 2,658
  • 2
  • 18
  • 30
5

npm install uuid

import it on your seeder file

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

Austin
  • 364
  • 1
  • 9
  • 21