Answers for "DatabaseError [SequelizeDatabaseError]: type "enum" already exists sync(force:false)"

0

DatabaseError [SequelizeDatabaseError]: type "enum" already exists sync(force:false)

//Add below script before require('sequelize')
//e.g.

require('./pgEnum-fix')
const sequelize = require('sequelize')


// pgEnum-fix.js
/* eslint-disable func-names */
/* eslint-disable no-restricted-syntax */
/* eslint-disable guard-for-in */
const PostgresQueryGenerator = require('sequelize/lib/dialects/postgres/query-generator');

PostgresQueryGenerator.prototype.pgEnum = function (tableName, attr, dataType, options) {
  const enumName = this.pgEnumName(tableName, attr, options);
  let values;

  if (dataType.values) {
    values = `ENUM(${dataType.values.map((value) => this.escape(value)).join(', ')})`;
  } else {
    // eslint-disable-next-line prefer-destructuring
    values = dataType.toString().match(/^ENUM\(.+\)/)[0];
  }

  if (_.isObject(tableName)) {
    normalizedTableName = `${tableName.schema}.${tableName.tableName}`;
  }

  let sql = `DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'enum_${normalizedTableName}_${attr}') THEN CREATE TYPE ${enumName} AS ${values}; END IF; END$$;`;
  
  if (!!options && options.force === true) {
    sql = this.pgEnumDrop(tableName, attr) + sql;
  }
  return sql;
};

PostgresQueryGenerator.prototype.changeColumnQuery = function (tableName, attributes) {
  const query = (subQuery) => `ALTER TABLE ${this.quoteTable(tableName)} ALTER COLUMN ${subQuery};`;
  const sql = [];
  for (const attributeName in attributes) {
    let definition = this.dataTypeMapping(tableName, attributeName, attributes[attributeName]);
    let attrSql = '';

    if (definition.includes('NOT NULL')) {
      attrSql += query(`${this.quoteIdentifier(attributeName)} SET NOT NULL`);

      definition = definition.replace('NOT NULL', '').trim();
    } else if (!definition.includes('REFERENCES')) {
      attrSql += query(`${this.quoteIdentifier(attributeName)} DROP NOT NULL`);
    }

    if (definition.includes('DEFAULT')) {
      attrSql += query(`${this.quoteIdentifier(attributeName)} SET DEFAULT ${definition.match(/DEFAULT ([^;]+)/)[1]}`);

      definition = definition.replace(/(DEFAULT[^;]+)/, '').trim();
    } else if (!definition.includes('REFERENCES')) {
      attrSql += query(`${this.quoteIdentifier(attributeName)} DROP DEFAULT`);
    }

    if (attributes[attributeName].startsWith('ENUM(')) {
      attrSql += this.pgEnum(tableName, attributeName, attributes[attributeName]);
      definition = definition.replace(/^ENUM\(.+\)/, this.pgEnumName(tableName, attributeName, { schema: false }));
      // definition += ` USING (${this.quoteIdentifier(attributeName)}::${this.pgEnumName(tableName, attributeName)})`;
    }

    if (definition.match(/UNIQUE;*$/)) {
      definition = definition.replace(/UNIQUE;*$/, '');
      attrSql += query(`ADD UNIQUE (${this.quoteIdentifier(attributeName)})`).replace('ALTER COLUMN', '');
    }

    if (definition.includes('REFERENCES')) {
      definition = definition.replace(/.+?(?=REFERENCES)/, '');
      attrSql += query(`ADD FOREIGN KEY (${this.quoteIdentifier(attributeName)}) ${definition}`).replace('ALTER COLUMN', '');
    } else {
      attrSql += query(`${this.quoteIdentifier(attributeName)} TYPE ${definition}`);
    }

    sql.push(attrSql);
  }

  return sql.join('');
};

module.exports = () => PostgresQueryGenerator;
Posted by: Guest on August-28-2021
0

UnhandledPromiseRejectionWarning: SequelizeDatabaseError: type "enum" already exists

Sequelize.sync({ force: true })
  .then(() => {
    console.log(`Database & tables generated!`)
});
Posted by: Guest on August-27-2021

Code answers related to "DatabaseError [SequelizeDatabaseError]: type "enum" already exists sync(force:false)"

Code answers related to "Javascript"

Browse Popular Code Answers by Language