{"record":{"id":"79e3496a7327784b","repo":"apache/cassandra","slug":"already-exists","errorCode":"ALREADY_EXISTS","errorMessage":"Cannot add already existing table \"%s\" to keyspace \"%s\"","messagePattern":"Cannot add already existing table \"(.+?)\" to keyspace \"(.+?)\"","errorType":"validation","errorClass":"AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java","lineNumber":165,"sourceCode":"        if (null == keyspace)\n            throw ire(\"Keyspace '%s' doesn't exist\", keyspaceName);\n\n        if (keyspace.replicationStrategy.hasTransientReplicas())\n            throw new InvalidRequestException(\"Materialized views are not supported on transiently replicated keyspaces\");\n\n        TableMetadata table = keyspace.tables.getNullable(tableName);\n        if (null == table)\n            throw ire(\"Base table '%s' doesn't exist\", tableName);\n\n        if (keyspace.hasTable(viewName))\n            throw ire(\"Cannot create materialized view '%s' - a table with the same name already exists\", viewName);\n\n        if (keyspace.hasView(viewName))\n        {\n            if (ifNotExists)\n                return schema;\n\n            throw new AlreadyExistsException(keyspaceName, viewName);\n        }\n\n        /*\n         * Base table validation\n         */\n\n        if (table.isCounter())\n            throw ire(\"Materialized views are not supported on counter tables\");\n\n        if (table.isView())\n            throw ire(\"Materialized views cannot be created against other materialized views\");\n\n        // Guardrails on table properties\n        Guardrails.tableProperties.guard(attrs.updatedProperties(), attrs::removeProperty, state);\n\n        // Guardrail to limit number of mvs per table\n        Iterable<ViewMetadata> tableViews = keyspace.views.forTable(table.id);\n        Guardrails.materializedViewsPerTable.guard(Iterables.size(tableViews) + 1,","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java#L147-L183","documentation":"A view with the given name already exists in the keyspace, so `CREATE MATERIALIZED VIEW` fails with AlreadyExistsException unless `IF NOT EXISTS` was used. This is the schema-creation uniqueness check on view names.","triggerScenarios":"`CREATE MATERIALIZED VIEW ks.existing_view AS ...` executed when `keyspace.hasView(viewName)` is true and `ifNotExists` is false.","commonSituations":"Re-running idempotent setup scripts without `IF NOT EXISTS`; name collision between a table and a view; CI pipelines replaying DDL.","solutions":["Add `IF NOT EXISTS` to the CREATE MATERIALIZED VIEW statement.","Choose a different view name.","If the existing view is stale, `DROP MATERIALIZED VIEW ks.view` first, then recreate it."],"exampleFix":"-- before\nCREATE MATERIALIZED VIEW ks.v AS SELECT * FROM ks.t WHERE x IS NOT NULL PRIMARY KEY (x, pk);\n\n-- after\nCREATE MATERIALIZED VIEW IF NOT EXISTS ks.v AS SELECT * FROM ks.t WHERE x IS NOT NULL PRIMARY KEY (x, pk);","handlingStrategy":"validation","validationCode":"Row exists = session.execute(\"SELECT view_name FROM system_schema.views WHERE keyspace_name=? AND view_name=?\", ks, view).one();\nif (exists != null && !ddl.toLowerCase().contains(\"if not exists\"))\n    throw new IllegalStateException(\"view already exists\");","typeGuard":null,"tryCatchPattern":"try { session.execute(createMv); }\ncatch (AlreadyExistsException e) {\n  // idempotent setup: safe to ignore when replaying DDL\n  log.info(\"MV {} already exists\", e.getTable());\n}","preventionTips":["Always use CREATE MATERIALIZED VIEW IF NOT EXISTS in setup/migration scripts","Check system_schema.views before creating","Use distinct naming conventions for views vs tables to avoid collisions"],"tags":["cassandra","materialized-view","already-exists","schema"],"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-14T21:17:11.552Z"}