{"id":"43c1d7849f068c78","repo":"sequelize/sequelize","slug":"primary-key-constraints-are-not-supported-by-thi","errorCode":null,"errorMessage":"Primary key constraints are not supported by ${this.dialect.name} dialect","messagePattern":"Primary key constraints are not supported by (.+?) dialect","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/abstract-dialect/query-generator-internal.ts","lineNumber":139,"sourceCode":"      case 'DEFAULT': {\n        if (!this.dialect.supports.constraints.default) {\n          throw new Error(`Default constraints are not supported by ${this.dialect.name} dialect`);\n        }\n\n        if (options.defaultValue === undefined) {\n          throw new Error('Default value must be specified for DEFAULT CONSTRAINT');\n        }\n\n        const constraintName = this.queryGenerator.quoteIdentifier(\n          options.name || `${table.tableName}_${fieldsSqlString}_df`,\n        );\n        constraintSnippet = `CONSTRAINT ${constraintName} DEFAULT (${this.queryGenerator.escape(options.defaultValue, options)}) FOR ${quotedFields[0]}`;\n        break;\n      }\n\n      case 'PRIMARY KEY': {\n        if (!this.dialect.supports.constraints.primaryKey) {\n          throw new Error(\n            `Primary key constraints are not supported by ${this.dialect.name} dialect`,\n          );\n        }\n\n        const constraintName = this.queryGenerator.quoteIdentifier(\n          options.name || `${table.tableName}_${fieldsSqlString}_pk`,\n        );\n        constraintSnippet = `CONSTRAINT ${constraintName} PRIMARY KEY (${fieldsSqlQuotedString})`;\n        if (options.deferrable) {\n          constraintSnippet += ` ${this.getDeferrableConstraintSnippet(options.deferrable)}`;\n        }\n\n        break;\n      }\n\n      case 'FOREIGN KEY': {\n        if (!this.dialect.supports.constraints.foreignKey) {\n          throw new Error(","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/sequelize/sequelize/blob/7e1deec499d5afbb8d1877c2f4d545cead1214ec/packages/core/src/abstract-dialect/query-generator-internal.ts#L121-L157","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Declare the primary key in the model definition (primaryKey: true on the attribute) and let sync/createTable emit it.","Use createTable with primaryKey in the schema instead of addConstraint.","Upgrade or replace the dialect adapter so supports.constraints.primaryKey is true."],"exampleFix":"// before\nqueryInterface.addConstraint('users', {\n  type: 'PRIMARY KEY',\n  fields: ['id'],\n});\n\n// after (column-level declaration at createTable)\nawait queryInterface.createTable('users', {\n  id: { type: Sequelize.INTEGER, primaryKey: true },\n  name: Sequelize.STRING,\n});","handlingStrategy":"fallback","validationCode":"function canAddPrimaryKeyConstraint(sequelize) {\n  return !['db2'].includes(sequelize.getDialect());\n}\n\nif (canAddPrimaryKeyConstraint(sequelize)) {\n  await queryInterface.addConstraint('users', { type: 'PRIMARY KEY', fields: ['id'] });\n} else {\n  await queryInterface.changeColumn('users', 'id', { primaryKey: true });\n}","typeGuard":"function dialectNeedsColumnLevelPK(name: string): boolean {\n  return name === 'db2';\n}","tryCatchPattern":"try {\n  await queryInterface.addConstraint('users', { type: 'PRIMARY KEY', fields: ['id'] });\n} catch (err) {\n  if (/Primary key constraints are not supported/.test(err.message)) {\n    await queryInterface.changeColumn('users', 'id', { primaryKey: true });\n  } else { throw err; }\n}","preventionTips":["Prefer declaring primaryKey in the model definition; reserve addConstraint for runtime additions.","Verify the dialect's support map when writing cross-database migrations."],"tags":["constraints","primary-key","dialect-support","schema"],"analyzedSha":"7e1deec499d5afbb8d1877c2f4d545cead1214ec","analyzedAt":"2026-08-03T18:58:44.549Z","schemaVersion":2}