From 54feb9686c54bb60c4c4ec71dc38da4d141022c5 Mon Sep 17 00:00:00 2001 From: hzhang Date: Sun, 24 May 2026 19:10:26 +0100 Subject: [PATCH] fix(cli): import all model modules so SA relationship resolution works MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hf-cli admin list crashed on prod with `KeyError: 'Agent'` because the CLI bypassed main.py's startup() which is the only place that imports every model module — User has a relationship target (`Agent`) that SQLAlchemy can't resolve unless its module is imported. Load them all up front in __main__.py (mirrors the main.py import block). Co-Authored-By: Claude Opus 4.7 (1M context) --- app/cli/__main__.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/app/cli/__main__.py b/app/cli/__main__.py index d4dd5dd..97cc6e0 100644 --- a/app/cli/__main__.py +++ b/app/cli/__main__.py @@ -2,6 +2,26 @@ import sys +def _load_all_models() -> None: + """Import every model module so SQLAlchemy's declarative registry + resolves cross-table relationships (e.g. User.role, User.agent). + + main.py's startup() does the same thing for the web server; the CLI + skips startup() but still queries User → would otherwise hit + `KeyError: 'Agent'` when SA tries to resolve relationship targets. + Keep this list in sync with main.py's startup import list. + """ + from app.models import ( # noqa: F401 + models, webhook, apikey, activity, milestone, notification, worklog, + monitor, role_permission, task, support, meeting, proposal, propose, + essential, agent, calendar, minimum_workload, schedule_type, + schedule_type_special_slot, oidc_settings, + ) + + +_load_all_models() + + USAGE = """Usage: hf-cli admin create-user --email [--username ] [--full-name ] [--password

] [--oidc-issuer --oidc-subject ]