sequelize/sequelize · error · Error

Primary key constraints are not supported by ${this.dialect.

Error message

Primary key constraints are not supported by ${this.dialect.name} dialect

What it means

The PRIMARY KEY constraint branch throws when this.dialect.supports.constraints.primaryKey is false. Because all mainstream dialects support primary keys, hitting this almost always indicates an experimental or stub dialect, or that the addConstraint path is being used where a column-level primaryKey declaration belongs.

Source

Thrown at packages/core/src/abstract-dialect/query-generator-internal.ts:139

      case 'DEFAULT': {
        if (!this.dialect.supports.constraints.default) {
          throw new Error(`Default constraints are not supported by ${this.dialect.name} dialect`);
        }

        if (options.defaultValue === undefined) {
          throw new Error('Default value must be specified for DEFAULT CONSTRAINT');
        }

        const constraintName = this.queryGenerator.quoteIdentifier(
          options.name || `${table.tableName}_${fieldsSqlString}_df`,
        );
        constraintSnippet = `CONSTRAINT ${constraintName} DEFAULT (${this.queryGenerator.escape(options.defaultValue, options)}) FOR ${quotedFields[0]}`;
        break;
      }

      case 'PRIMARY KEY': {
        if (!this.dialect.supports.constraints.primaryKey) {
          throw new Error(
            `Primary key constraints are not supported by ${this.dialect.name} dialect`,
          );
        }

        const constraintName = this.queryGenerator.quoteIdentifier(
          options.name || `${table.tableName}_${fieldsSqlString}_pk`,
        );
        constraintSnippet = `CONSTRAINT ${constraintName} PRIMARY KEY (${fieldsSqlQuotedString})`;
        if (options.deferrable) {
          constraintSnippet += ` ${this.getDeferrableConstraintSnippet(options.deferrable)}`;
        }

        break;
      }

      case 'FOREIGN KEY': {
        if (!this.dialect.supports.constraints.foreignKey) {
          throw new Error(

View on GitHub (pinned to 7e1deec499)

Solutions

  1. Declare the primary key in the model definition (primaryKey: true on the attribute) and let sync/createTable emit it.
  2. Use createTable with primaryKey in the schema instead of addConstraint.
  3. Upgrade or replace the dialect adapter so supports.constraints.primaryKey is true.

Example fix

// before
queryInterface.addConstraint('users', {
  type: 'PRIMARY KEY',
  fields: ['id'],
});

// after (column-level declaration at createTable)
await queryInterface.createTable('users', {
  id: { type: Sequelize.INTEGER, primaryKey: true },
  name: Sequelize.STRING,
});
Defensive patterns

Strategy: fallback

Validate before calling

function canAddPrimaryKeyConstraint(sequelize) {
  return !['db2'].includes(sequelize.getDialect());
}

if (canAddPrimaryKeyConstraint(sequelize)) {
  await queryInterface.addConstraint('users', { type: 'PRIMARY KEY', fields: ['id'] });
} else {
  await queryInterface.changeColumn('users', 'id', { primaryKey: true });
}

Type guard

function dialectNeedsColumnLevelPK(name: string): boolean {
  return name === 'db2';
}

Try / catch

try {
  await queryInterface.addConstraint('users', { type: 'PRIMARY KEY', fields: ['id'] });
} catch (err) {
  if (/Primary key constraints are not supported/.test(err.message)) {
    await queryInterface.changeColumn('users', 'id', { primaryKey: true });
  } else { throw err; }
}

Prevention

When it happens

Trigger: Calling addConstraint type 'PRIMARY KEY' against a dialect whose support flag is false, or against a dialect adapter that has not declared primaryKey constraint support.

Common situations: Using a community/experimental dialect. Trying to add a PK via the constraint API on a dialect that prefers column-level primaryKey in the model definition.

Related errors


AI-assisted analysis of sequelize/sequelize@7e1deec499 (2026-08-03). Data as JSON: /data/errors/43c1d7849f068c78.json. Report an issue: GitHub.