{"record":{"id":"5df0861cd011dad5","repo":"apache/iceberg","slug":"cannot-create-table-s-as-the-namespace-does-not-e-5df086","errorCode":null,"errorMessage":"Cannot create table %s as the namespace does not exist","messagePattern":"Cannot create table (.+?) as the namespace does not exist","errorType":"exception","errorClass":"NoSuchNamespaceException","httpStatus":null,"severity":"error","filePath":"spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/actions/BaseTableCreationSparkAction.java","lineNumber":164,"sourceCode":"        \"Cannot create Iceberg table in non-Iceberg Catalog. \"\n            + \"Catalog '%s' was of class '%s' but '%s' or '%s' are required\",\n        catalog.name(),\n        catalog.getClass().getName(),\n        SparkSessionCatalog.class.getName(),\n        SparkCatalog.class.getName());\n\n    return (StagingTableCatalog) catalog;\n  }\n\n  protected StagedSparkTable stageDestTable() {\n    try {\n      Map<String, String> props = destTableProps();\n      StructType schema = sourceTable.schema();\n      Transform[] partitioning = sourceTable.partitioning();\n      return (StagedSparkTable)\n          destCatalog().stageCreate(destTableIdent(), schema, partitioning, props);\n    } catch (org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException e) {\n      throw new NoSuchNamespaceException(\n          \"Cannot create table %s as the namespace does not exist\", destTableIdent());\n    } catch (org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException e) {\n      throw new AlreadyExistsException(\n          \"Cannot create table %s as it already exists\", destTableIdent());\n    }\n  }\n\n  protected void ensureNameMappingPresent(Table table) {\n    if (!table.properties().containsKey(TableProperties.DEFAULT_NAME_MAPPING)) {\n      NameMapping nameMapping = MappingUtil.create(table.schema());\n      String nameMappingJson = NameMappingParser.toJson(nameMapping);\n      table.updateProperties().set(TableProperties.DEFAULT_NAME_MAPPING, nameMappingJson).commit();\n    }\n  }\n\n  protected String getMetadataLocation(Table table) {\n    String defaultValue =\n        LocationUtil.stripTrailingSlash(table.location()) + \"/\" + ICEBERG_METADATA_FOLDER;","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/actions/BaseTableCreationSparkAction.java#L146-L182","documentation":"Wraps Spark's NoSuchNamespaceException raised during stageCreate when staging the destination Iceberg table: the target namespace in the destination catalog does not exist. Iceberg actions do not auto-create namespaces.","triggerScenarios":"Calling migrateTable/snapshotTable with a destination identifier whose namespace has not been created in the destination catalog, so destCatalog().stageCreate(...) throws NoSuchNamespaceException.","commonSituations":"Typo in the destination namespace; forgetting to CREATE NAMESPACE in the target catalog; pointing at a fresh catalog (e.g. REST/Hadoop) that has no pre-created namespaces.","solutions":["Create the destination namespace first: CREATE NAMESPACE <ns> in the destination catalog","Check the destination identifier spelling and the dest catalog config (spark.sql.catalog.<name>)","Run the action again after the namespace exists"],"exampleFix":"// before\nspark.sql(\"CREATE TABLE iceberg_db.migrated_tbl ...\"); // namespace missing\n// after\nspark.sql(\"CREATE NAMESPACE IF NOT EXISTS iceberg_db\");\nSparkActions.get(spark).migrateTable(\"db.tbl\").as(\"iceberg_db.migrated_tbl\").execute();","handlingStrategy":"validation","validationCode":"Namespace ns = Namespace.of(parts);\nif (!destCatalog.namespaceExists(ns)) { spark.sql(\"CREATE NAMESPACE IF NOT EXISTS \" + nsString); }","typeGuard":null,"tryCatchPattern":"try { action.execute(); } catch (NoSuchNamespaceException e) { createNamespace(); }","preventionTips":["Create destination namespaces ahead of time","Validate identifier spelling","Pre-provision namespaces in new catalogs"],"tags":["spark","namespace","staged-create"],"backgroundTag":"entity-not-found","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}