WalksAway
WalksAway

Reputation: 2829

How to seed uuidv4 with sequelize

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()
    }]);

Upvotes: 15

Views: 15279

Answers (3)

Austin
Austin

Reputation: 424

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

Upvotes: 7

Hichame Yessou
Hichame Yessou

Reputation: 2718

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,
    ...
}

Upvotes: 8

Amir Sasson
Amir Sasson

Reputation: 11543

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()
      }],
 {});

Upvotes: 14

Related Questions