Skip to content

refactor: 去除 UserMixin 审计字段外键约束以消除循环外键 - #541

Merged
fastapiadmin merged 1 commit into
fastapiadmin:devfrom
Echo-Note:bugfix/remove-audit-fk-cycle
Oct 22, 2026
Merged

fastapiadmin merged 1 commit into
fastapiadmin:devfrom
Echo-Note:bugfix/remove-audit-fk-cycle

Conversation

@Echo-Note

Copy link
Copy Markdown

背景

修复循环外键问题,关联 issue #540。

sys_user.dept_id → sys_dept.id 与 sys_dept 审计字段(created_id/updated_id/deleted_id → sys_user.id,来自 UserMixin)构成循环外键。原 dept_id 用 use_alter=True 规避,但 use_alter 在 Alembic autogenerate 下会被内联进 op.create_table,PostgreSQL 编译时跳过导致外键静默丢失(详见 #540)。

改动

  1. UserMixin 三个审计字段(created_id/updated_id/deleted_id)移除指向 sys_user 的数据库级外键约束(ondelete=SET NULL 已允许悬空,审计字段本就不需要强引用语义)。
  2. created_by/updated_by/deleted_by 关系改用显式 primaryjoin 保持可查。
  3. dept_id 移除 use_alter=True(循环消除后不再需要)。

效果

  • 循环依赖消失,Alembic autogenerate 无需 use_alter 即可正确生成迁移。
  • 已在 fork 验证:ruff check 通过、pytest(PostgreSQL)74 passed、configure_mappers 正常。

关联

sys_user.dept_id 与 sys_dept(UserMixin 审计字段)互为外键形成循环依赖。原 dept_id 用 use_alter=True 规避,但 use_alter 在 Alembic autogenerate 下会被内联进 op.create_table,PostgreSQL 编译时跳过导致外键静默丢失。

移除 created_id/updated_id/deleted_id 三个审计字段指向 sys_user 的外键约束(ondelete=SET NULL 已允许悬空,审计字段无需强引用),并为 created_by/updated_by/deleted_by 关系显式补充 primaryjoin。循环依赖消除后,dept_id 的 use_alter 亦可移除,autogenerate 可正确生成迁移。
@fastapiadmin
fastapiadmin merged commit 45a8ade into fastapiadmin:dev Oct 22, 2026
2 checks passed
xiaoyu-awk pushed a commit to xiaoyu-awk/FastapiAdmin that referenced this pull request Oct 23, 2026
原 PR fastapiadmin#543 的实现,因其与 fastapiadmin#541 同改 base_model.py 的 import 行产生冲突,此处为基于最新
dev 的 rebase 版本,改动内容与原提交一致(提交作者保留为 Echo-Note)。

- PostgreSQL:JSONB;SQLite / MySQL:通用 JSON(TypeEngine.with_variant)
- 验证:三方言 DDL(JSONB / JSON / JSON)、sqlite create_all 建表 + JSON 读写、
  ruff / basedpyright 0 errors / pytest 97 passed

关联 issue fastapiadmin#542:本 PR 提供统一入口;生成器 jsonb 映射与方言类型 import 的修复留作后续。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants