question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Seeding ARRAY(ENUM)

See original GitHub issue

Issue Description

I am trying to seed an ARRAY(ENUM), but I have not been able to figure out how to do so. I also haven’t been able to find any other issue/documentation that showed how to do this.

I keep getting this error: ERROR: column "roles" is of type "enum_Users_roles"[] but expression is of type text[]

This is my migration script:

return queryInterface.createTable('Users', {
     id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },  

      email: {
        type: Sequelize.STRING,
	      allowNull: false
      },  

      password: {
        type: Sequelize.STRING,
	      allowNull: false
      },  

      roles: {
        type: Sequelize.ARRAY(Sequelize.ENUM({
	        values: ['user', 'setter', 'admin']
        })),
	      allowNull: false
      },  

      public_id: {
        type: Sequelize.UUID,
	      defaultValue: Sequelize.UUIDV4,
	      allowNull: false
      },  

      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },  

      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }  
    })

And here is my seed file:

'use strict';
const faker = require('faker');

module.exports = {
  up: async (queryInterface, Sequelize) => {
	  return queryInterface.bulkInsert('Users', [
		  {
			  email: faker.internet.email(),
			  roles: ['user'],
			  password: "hash",
			  public_id: faker.random.uuid(),
			  created_at: new Date(),
			  updated_at: new Date()
		  }
	  ]);
  },

  down: (queryInterface, Sequelize) => {

    return queryInterface.bulkDelete('Users', null, {});
  }
};

StackOverflow / Slack attempts

I asked on StackOverflow, but I have not gotten any response (it has only been a day) https://stackoverflow.com/questions/58326145/sequelize-seeding-arrayenum

Issue Template Checklist

Is this issue dialect-specific?

  • No. This issue is relevant to Sequelize as a whole.
  • Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ
  • I don’t know.

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don’t know how to start, I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
huntonascommented, Oct 16, 2019

@papb sorry. Didn’t mean to close it on you. The stackoverflow question got a some views and a few thumbs up, so I guess I am not the only one trying to figure it out. Probably a good call to add it to docs. Thanks

3reactions
sushantdhimancommented, Oct 16, 2019

You can use this code

class Item extends Sequelize.Model { }

Item.init({
  name: { type: DataTypes.STRING },
  values: {
    type: DataTypes.ARRAY(DataTypes.ENUM({
      values: ['a', 'b']
    }))
  }
}, {
  sequelize,
  timestamps: true
})

sequelize.sync({ force: true }).then(async () => {
  await sequelize.queryInterface.bulkInsert('Items', [
    {
      name: 'xyz',
      values: sequelize.literal(`ARRAY['a']::"enum_Items_values"[]`),
      createdAt: new Date(),
      updatedAt: new Date()
    }
  ]);
});
Executing (default): INSERT INTO "Items" ("name","values","createdAt","updatedAt") VALUES ('xyz',ARRAY['a']::"enum_Items_values"[],'2019-10-16 16:44:30.953 +00:00','2019-10-16 16:44:30.953 +00:00');
Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize Seeding ARRAY(ENUM) - Stack Overflow
I cannot seem to figure out how to seed ARRAY(ENUM) using Sequelize. When I am registering a user via my app, I can...
Read more >
How to seed schema containing enums - Questions - Prisma 1
Howdy! How do we seed a schema containing enums? The above throws the error: Error: Variable '$data' expected value of type 'PlaceCreateInput!'
Read more >
[Solved]-Sequelize Seeding ARRAY(ENUM)-node.js
Coding example for the question Sequelize Seeding ARRAY(ENUM)-node.js. ... Obviously you'll want to change the array values and enum values.
Read more >
Extension Method to Seed Enum Values into Database Table
When an enum is persisted in a database table, it is better if the Code First entity doesn't use a (SQL Server) IDENTITY...
Read more >
How to Use Enums in Rails | Saeloun Blog
Since we have used arrays for declaring enums, draft will be mapped to 0 in the database, published will be mapped to 1,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found