{"record":{"id":"eb41d6b07bfdaf04","repo":"mlflow/mlflow","slug":"target-database-is-not-empty-table-table-has","errorCode":null,"errorMessage":"Target database is not empty: table '{table}' has {count} rows. Migration requires an empty database.","messagePattern":"Target database is not empty: table '(.+?)' has (.+?) rows\\. Migration requires an empty database\\.","errorType":"exception","errorClass":"MlflowException","httpStatus":null,"severity":"error","filePath":"mlflow/store/fs2db/__init__.py","lineNumber":38,"sourceCode":"        d.name.isdigit() or d.name in {\".trash\", \"models\"} for d in source.iterdir() if d.is_dir()\n    )\n    if has_experiment_dirs:\n        return source\n\n    raise MlflowException(f\"Cannot find mlruns directory in '{source}'\")\n\n\ndef _assert_empty_db(engine) -> None:\n    from sqlalchemy import text\n\n    with engine.connect() as conn:\n        for table in (\"experiments\", \"runs\", \"registered_models\"):\n            try:\n                count = conn.execute(text(f\"SELECT COUNT(*) FROM {table}\")).scalar()\n            except Exception:\n                continue\n            if count > 0:\n                raise MlflowException(\n                    f\"Target database is not empty: table '{table}' has {count} rows. \"\n                    \"Migration requires an empty database.\"\n                )\n\n\n_ROW_COUNT_QUERIES: dict[str, str] = {\n    \"experiments\": \"SELECT COUNT(*) FROM experiments\",\n    \"experiment_tags\": \"SELECT COUNT(*) FROM experiment_tags\",\n    \"runs\": \"SELECT COUNT(*) FROM runs\",\n    \"params\": \"SELECT COUNT(*) FROM params\",\n    \"tags\": \"SELECT COUNT(*) FROM tags\",\n    \"metrics\": \"SELECT COUNT(*) FROM metrics\",\n    \"latest_metrics\": \"SELECT COUNT(*) FROM latest_metrics\",\n    \"datasets\": \"SELECT COUNT(*) FROM datasets\",\n    \"inputs\": \"SELECT COUNT(*) FROM inputs WHERE source_type = 'DATASET'\",\n    \"input_tags\": \"SELECT COUNT(*) FROM input_tags\",\n    \"outputs\": \"SELECT COUNT(*) FROM inputs WHERE source_type = 'RUN_OUTPUT'\",\n    \"traces\": \"SELECT COUNT(*) FROM trace_info\",","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/mlflow/mlflow/blob/6a27f2decc0b76eb1b54af31849784addb357dbc/mlflow/store/fs2db/__init__.py#L20-L56","documentation":"fs2db requires the target SQL database to be completely empty so migrated records don't collide with existing rows. Before migrating, _assert_empty_db counts rows in the experiments, runs, and registered_models tables; any table with rows aborts the migration with MlflowException. Tables that don't exist are skipped silently.","triggerScenarios":"Running migrate() against a SQL database that already contains tracking or registry data — e.g. a DB previously used by an MLflow server, or a partially completed earlier migration.","commonSituations":"Reusing an existing mlflow.db file; re-running a failed migration after some rows were committed; pointing at a shared staging database.","solutions":["Point the migration at a fresh, empty database (or delete the old db file).","If re-running after a failure, drop and recreate the schema before migrating.","Back up the existing database first, then clear it if the old data is no longer needed."],"exampleFix":"# before\nmigrate(engine, \"mlruns\")  # mlflow.db has old data\n# after\nrm mlflow.db && mlflow db upgrade sqlite:///mlflow.db  # fresh empty schema\nmigrate(engine, \"mlruns\")","handlingStrategy":"validation","validationCode":"from sqlalchemy import create_engine, text\nwith create_engine(db_uri).connect() as c:\n    for t in (\"experiments\", \"runs\", \"registered_models\"):\n        try:\n            n = c.execute(text(f\"SELECT COUNT(*) FROM {t}\")).scalar()\n        except Exception:\n            continue\n        if n:\n            raise SystemExit(f\"DB not empty: {t} has {n} rows; use a fresh database\")","typeGuard":null,"tryCatchPattern":"try:\n    migrate(engine, source)\nexcept MlflowException as e:\n    if \"Target database is not empty\" in str(e):\n        raise SystemExit(\"Point migration at an empty database or clear the existing one\")\n    raise","preventionTips":["Create a brand-new database file/schema for fs2db migrations","Back up existing DBs before clearing","Never reuse a live MLflow server's database as a migration target"],"tags":["migration","database","precondition"],"backgroundTag":"database-not-empty","analyzedSha":"6a27f2decc0b76eb1b54af31849784addb357dbc","analyzedAt":"2026-08-29T20:54:51.419Z","schemaVersion":2},"datasetVersion":"2026-08-29T22:17:34.462Z"}