Scott
Scott

Reputation: 97

SequelizeDatabaseError: dropping non-existent ENUM types, but they actually exist

When attempting to undo sequelize migrations in my heroku-postgres database, I get a SequelizeDatabaseError, due to ENUM types supposedly not existing.

I have several tables/models with ENUM data types. In the down migrations, I attempt to drop the table and then drop the table's associated enum data types as well. So far, only one migration seems to properly be applied in both directions.

I have tried manually dropping every type and then migrating all, and undoing all, but it always fails on the same models, saying the ENUM types do not exist (when they clearly have been created in the up migrations).

Here is a model which has a failing down migration.

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('PatientContacts', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      phoneHome: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      phoneWork: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      phoneMobile: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      phonePreferred: {
        type: Sequelize.ENUM,
        values: ['Home','Work','Mobile', null]
      },
      smsNotifications: {
        type: Sequelize.ENUM,
        values: [true, false, null]
      },
      email: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      emailConsent: {
        type: Sequelize.ENUM,
        values: [true, false, null]
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('PatientContacts')
    .then(() => queryInterface.sequelize.query('DROP TYPE "public"."enum_PatientContacts_emailConsent;"'))
    .then(() => queryInterface.sequelize.query('DROP TYPE "public"."enum_PatientContacts_phonePreferred;"'))
    .then(() => queryInterface.sequelize.query('DROP TYPE "public"."enum_PatientContacts_smsNotifications;"'))
    .catch(err => {
      console.log(err);
      throw new Error(err);
    })
  }
};

I would expect the down migration to execute the raw queries in the callbacks. Yet, the error is as described above:

Error: SequelizeDatabaseError: type "public.enum_PatientContacts_emailConsent;" does not exist
at queryInterface.dropTable.then.then.then.catch.err (/app/migrations/20181213023202-create-PatientContact.js:56:13)
at tryCatcher (/app/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/app/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/app/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/app/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/app/node_modules/bluebird/js/release/promise.js:690:18)
at _drainQueueStep (/app/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/app/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/app/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/app/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:693:18)
at tryOnImmediate (timers.js:664:5)
at processImmediate (timers.js:646:5)

I should mention that prefacing the type's name with "public" was just in my latest attempt. Removing it also yields the same result.

Upvotes: 1

Views: 1207

Answers (1)

Scott
Scott

Reputation: 97

I'm very disappointed that I was stuck on this problem for hours.

The semi-colon to end the raw query was meant to come after the double quote ending the type name, but before the single quote ending the entire sql query itself.

Upvotes: 1

Related Questions