apache/cassandra · error · AlreadyExistsException

ALREADY_EXISTS

ALREADY_EXISTS

Error message

Cannot add already existing table "%s" to keyspace "%s"

What it means

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.

Source

Thrown at src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java:165

        if (null == keyspace)
            throw ire("Keyspace '%s' doesn't exist", keyspaceName);

        if (keyspace.replicationStrategy.hasTransientReplicas())
            throw new InvalidRequestException("Materialized views are not supported on transiently replicated keyspaces");

        TableMetadata table = keyspace.tables.getNullable(tableName);
        if (null == table)
            throw ire("Base table '%s' doesn't exist", tableName);

        if (keyspace.hasTable(viewName))
            throw ire("Cannot create materialized view '%s' - a table with the same name already exists", viewName);

        if (keyspace.hasView(viewName))
        {
            if (ifNotExists)
                return schema;

            throw new AlreadyExistsException(keyspaceName, viewName);
        }

        /*
         * Base table validation
         */

        if (table.isCounter())
            throw ire("Materialized views are not supported on counter tables");

        if (table.isView())
            throw ire("Materialized views cannot be created against other materialized views");

        // Guardrails on table properties
        Guardrails.tableProperties.guard(attrs.updatedProperties(), attrs::removeProperty, state);

        // Guardrail to limit number of mvs per table
        Iterable<ViewMetadata> tableViews = keyspace.views.forTable(table.id);
        Guardrails.materializedViewsPerTable.guard(Iterables.size(tableViews) + 1,

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Add `IF NOT EXISTS` to the CREATE MATERIALIZED VIEW statement.
  2. Choose a different view name.
  3. If the existing view is stale, `DROP MATERIALIZED VIEW ks.view` first, then recreate it.

Example fix

-- before
CREATE MATERIALIZED VIEW ks.v AS SELECT * FROM ks.t WHERE x IS NOT NULL PRIMARY KEY (x, pk);

-- after
CREATE MATERIALIZED VIEW IF NOT EXISTS ks.v AS SELECT * FROM ks.t WHERE x IS NOT NULL PRIMARY KEY (x, pk);
Defensive patterns

Strategy: validation

Validate before calling

Row exists = session.execute("SELECT view_name FROM system_schema.views WHERE keyspace_name=? AND view_name=?", ks, view).one();
if (exists != null && !ddl.toLowerCase().contains("if not exists"))
    throw new IllegalStateException("view already exists");

Try / catch

try { session.execute(createMv); }
catch (AlreadyExistsException e) {
  // idempotent setup: safe to ignore when replaying DDL
  log.info("MV {} already exists", e.getTable());
}

Prevention

When it happens

Trigger: `CREATE MATERIALIZED VIEW ks.existing_view AS ...` executed when `keyspace.hasView(viewName)` is true and `ifNotExists` is false.

Common situations: Re-running idempotent setup scripts without `IF NOT EXISTS`; name collision between a table and a view; CI pipelines replaying DDL.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/79e3496a7327784b. Report an issue: GitHub.