{"record":{"id":"91996316367c2ad0","repo":"apache/cassandra","slug":"column-with-name-s-already-exists","errorCode":null,"errorMessage":"Column with name '%s' already exists","messagePattern":"Column with name '(.+?)' already exists","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/AlterTableStatement.java","lineNumber":356,"sourceCode":"                           .withSwapped(viewsBuilder.build());\n        }\n\n        private void addColumn(KeyspaceMetadata keyspace,\n                               TableMetadata table,\n                               Column column,\n                               boolean ifColumnNotExists,\n                               TableMetadata.Builder tableBuilder,\n                               Views.Builder viewsBuilder)\n        {\n            ColumnIdentifier name = column.name;\n            AbstractType<?> type = column.type.prepare(keyspaceName, keyspace.types).getType();\n            boolean isStatic = column.isStatic;\n            ColumnMask mask = column.mask == null ? null : column.mask.prepare(keyspaceName, tableName, name, type, keyspace.userFunctions);\n            ColumnConstraints columnConstraints = column.constraints == null ? ColumnConstraints.NO_OP : column.constraints.prepare(name);\n\n            if (null != tableBuilder.getColumn(name)) {\n                if (!ifColumnNotExists)\n                    throw ire(\"Column with name '%s' already exists\", name);\n                return;\n            }\n\n            if (type.isCounter() && (table.params.transactionalMode.accordIsEnabled || table.params.transactionalMigrationFrom.migratingFromAccord()))\n                throw ire(format(ACCORD_COUNTER_COLUMN_UNSUPPORTED, keyspaceName, tableName, table.params.transactionalMode, table.params.transactionalMigrationFrom));\n\n            if (table.isCompactTable())\n                throw ire(\"Cannot add new column to a COMPACT STORAGE table\");\n\n            if (isStatic && table.clusteringColumns().isEmpty())\n                throw ire(\"Static columns are only useful (and thus allowed) if the table has at least one clustering column\");\n\n            // check for nested non-frozen UDTs or collections in a non-frozen UDT\n            if (type.isUDT() && type.isMultiCell())\n            {\n                for (AbstractType<?> fieldType : ((UserType) type).fieldTypes())\n                {\n                    if (fieldType.isMultiCell())","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/AlterTableStatement.java#L338-L374","documentation":"Thrown by ALTER TABLE ... ADD when a column with the given name already exists on the table and IF NOT EXISTS was not specified. The builder detects the collision via tableBuilder.getColumn(name) and rejects the statement instead of silently overwriting.","triggerScenarios":"'ALTER TABLE ks.tbl ADD col type' where a column of that name already exists and ifColumnNotExists is false; re-running migration scripts that add columns; adding a column that differs only by case in an unquoted statement","commonSituations":"Non-idempotent schema migration executed twice; drift between environments where the column already exists in one; ORMs/tools that re-emit ADD COLUMN on startup.","solutions":["Use IF NOT EXISTS: ALTER TABLE ks.tbl ADD IF NOT EXISTS col type","Verify existing column types match your intent: SELECT * FROM system_schema.columns WHERE keyspace_name=? AND table_name=?","Make migrations idempotent or track applied migrations in a schema-version table"],"exampleFix":"// before\nsession.execute(\"ALTER TABLE my_app.events ADD description text\");\n// after\nsession.execute(\"ALTER TABLE my_app.events ADD IF NOT EXISTS description text\");","handlingStrategy":"validation","validationCode":"boolean exists = session.execute(\"SELECT column_name FROM system_schema.columns WHERE keyspace_name=? AND table_name=? AND column_name=?\", ks, table, col).one() != null;\nif (exists) log.info(\"Column already present; skipping ADD\");","typeGuard":null,"tryCatchPattern":"try {\n    session.execute(alter);\n} catch (InvalidRequestException e) {\n    if (e.getMessage().contains(\"already exists\"))\n        log.info(\"Column exists, continuing: {}\", alter);\n    else throw e;\n}","preventionTips":["Always use ADD IF NOT EXISTS in migrations","Keep a schema-version/migration ledger so steps run once","Compare intended vs existing column types when a name collides"],"tags":["cassandra","cql","ddl","duplicate-column"],"backgroundTag":"file-already-exists","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}