Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Fix: handle tenant_llm without id (#18541)

Lynn618c4599b10e
diff --git a/tools/scripts/mysql_migration.py b/tools/scripts/mysql_migration.py
index bf7973f2f6..ea400f25dc 100644
--- a/tools/scripts/mysql_migration.py
+++ b/tools/scripts/mysql_migration.py
@@ -772,9 +772,11 @@ class TenantModelStage(MigrationStage):
# We cannot JOIN tenant_model_instance on api_key directly because the instance
# stage deduped api_keys (stripping is_tools), so a plain SQL equality won't
# match records whose api_key was merged. Count at the provider level instead.
+ # Use a constant instead of tl.id: very old tenant_llm tables may lack the
+ # id column, and the count only cares about the number of rows.
cursor = self.db.execute_sql(
f"SELECT COUNT(*) FROM ("
- f" SELECT tl.id "
+ f" SELECT 1 "
f" FROM tenant_llm tl "
f" INNER JOIN tenant_model_provider tmp ON tmp.tenant_id = tl.tenant_id AND tmp.provider_name = tl.llm_factory "
f" WHERE {status_condition} "
@@ -818,6 +820,14 @@ class TenantModelStage(MigrationStage):
logger.info("[CREATE TABLE ONLY] Target table created/verified, skipping data migration")
return 0, self.target_tables
+ # Very old tenant_llm tables may lack the id column, while the migration
+ # SELECT below relies on it (source_id for audit logging). Add an
+ # auto-increment id column in place before running the data comparison
+ # and inserts. Fail hard when it cannot be ensured: silently returning
+ # would let the runner mark this stage as completed without migrating.
+ if not self._ensure_tenant_llm_id_column():
+ raise RuntimeError("tenant_llm has no 'id' column and it could not be added; aborting tenant_model stage")
+
status_condition = self._build_status_condition()
# Load all tenant_model_instance records into memory for Python-level matching.
@@ -896,6 +906,35 @@ class TenantModelStage(MigrationStage):
return rows_inserted, self.target_tables
+ def _ensure_tenant_llm_id_column(self) -> bool:
+ """Ensure tenant_llm has an id column, adding an auto-increment one if missing.
+
+ Very old tenant_llm tables may predate the id column, while the migration
+ SELECT relies on it (tl.id is carried as source_id for audit logging).
+ The id is only used as a unique audit key, so it is added as an
+ AUTO_INCREMENT UNIQUE column rather than PRIMARY KEY: old tables may
+ already have another primary key. MySQL back-fills existing rows with
+ sequential values when adding an AUTO_INCREMENT column.
+
+ Returns:
+ True if the column exists (or was added successfully), False otherwise.
+ """
+ if self.db.column_exists("tenant_llm", "id"):
+ return True
+
+ logger.warning("Source table 'tenant_llm' has no 'id' column, adding auto-increment id column")
+ if self.dry_run:
+ logger.info("[DRY RUN] Would ALTER TABLE tenant_llm to add auto-increment id column")
+ return False
+
+ try:
+ self.db.execute_sql("ALTER TABLE tenant_llm ADD COLUMN id BIGINT AUTO_INCREMENT UNIQUE")
+ except Exception as e:
+ logger.error(f"Failed to add auto-increment id column to tenant_llm: {e}")
+ return False
+ logger.info("Added auto-increment id column to tenant_llm")
+ return True
+
def _build_instance_lookup(self) -> dict:
"""Load all tenant_model_instance records, indexed by (provider_id, canonical_api_key).