{"id":"b7f22d9c06372299","repo":"knex/knex","slug":"dropuniqueifexists-is-not-supported-by-redshift","errorCode":null,"errorMessage":".dropUniqueIfExists() is not supported by redshift","messagePattern":"\\.dropUniqueIfExists\\(\\) is not supported by redshift","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/dialects/redshift/schema/redshift-tablecompiler.js","lineNumber":35,"sourceCode":"    );\n  }\n\n  dropIndex(columns, indexName) {\n    this.client.logger.warn(\n      'Redshift does not support the deletion of indexes.'\n    );\n  }\n\n  dropPrimaryIfExists() {\n    throw new Error('.dropPrimaryIfExists() is not supported by redshift');\n  }\n\n  dropForeignIfExists() {\n    throw new Error('.dropForeignIfExists() is not supported by redshift');\n  }\n\n  dropUniqueIfExists() {\n    throw new Error('.dropUniqueIfExists() is not supported by redshift');\n  }\n\n  // TODO: have to disable setting not null on columns that already exist...\n\n  // Adds the \"create\" query to the query sequence.\n  createQuery(columns, ifNot, like) {\n    const createStatement = ifNot\n      ? 'create table if not exists '\n      : 'create table ';\n    const columnsSql = ' (' + columns.sql.join(', ') + this._addChecks() + ')';\n    let sql =\n      createStatement +\n      this.tableName() +\n      (like && this.tableNameLike()\n        ? ' (like ' + this.tableNameLike() + ')'\n        : columnsSql);\n    if (this.single.inherits)\n      sql += ` like (${this.formatter.wrap(this.single.inherits)})`;","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/knex/knex/blob/e25d54bcb707714a17f5a5744eba5c4246bb4d1d/lib/dialects/redshift/schema/redshift-tablecompiler.js#L17-L53","documentation":"redshift-tablecompiler.js:34 throws because Redshift does not support IF EXISTS on DROP UNIQUE. The PG base class exposes dropUniqueIfExists, but Redshift's ALTER TABLE grammar lacks the optional-existence form, so the Redshift compiler overrides the method to reject the call.","triggerScenarios":"Calling table.dropUniqueIfExists() in a migration against Redshift: schema.table('t', t => t.dropUniqueIfExists(['col'], 'u_t_col')). Also via any introspection-driven helper that prefers the IfExists form.","commonSituations":"Idempotent migration suites shared with Postgres; tools that always use the *IfExists variants; refactoring indexes on a Redshift warehouse using PG-oriented migration code.","solutions":["Introspect pg_constraint/information_schema first and call table.dropUnique() only if it exists.","Branch by dialect: Redshift -> dropUnique, PG -> dropUniqueIfExists.","Wrap the drop in try/catch on Redshift when existence cannot be guaranteed."],"exampleFix":"// before\nschema.table('t', t => t.dropUniqueIfExists(['email'], 'u_t_email'));\n\n// after (dialect branch)\nschema.table('t', t => {\n  if (knex.client.dialect === 'redshift') t.dropUnique(['email'], 'u_t_email');\n  else t.dropUniqueIfExists(['email'], 'u_t_email');\n});","handlingStrategy":"validation","validationCode":"async function dropUniqueSafe(knex, table, name) {\n  if (knex.client.dialect === 'redshift') {\n    const c = await knex('information_schema.table_constraints')\n      .where({ table_name: table, constraint_name: name, constraint_type: 'UNIQUE' }).first();\n    if (c) await knex.schema.table(table, t => t.dropUnique([], name));\n  } else {\n    await knex.schema.table(table, t => t.dropUniqueIfExists([], name));\n  }\n}","typeGuard":"function isRedshiftDialect(client) {\n  return /redshift/i.test((client && client.dialect) || '');\n}","tryCatchPattern":"try {\n  schema.table('t', t => t.dropUniqueIfExists(['email'], 'u_t_email'));\n} catch (err) {\n  if (/dropUniqueIfExists\\(\\) is not supported by redshift/i.test(err.message)) {\n    schema.table('t', t => t.dropUnique(['email'], 'u_t_email'));\n  } else throw err;\n}","preventionTips":["Introspect constraints before dropping on Redshift.","Branch schema helpers by dialect in shared migrations.","Add a migration lint that flags *IfExists calls for Redshift."],"tags":["redshift","schema","migration","unique","ddl"],"analyzedSha":"e25d54bcb707714a17f5a5744eba5c4246bb4d1d","analyzedAt":"2026-08-03T18:35:32.148Z","schemaVersion":2}