Repository navigation
refactor: 去除 UserMixin 审计字段外键约束以消除循环外键 - #541
Merged
fastapiadmin merged 1 commit intoOct 22, 2026
Merged
Conversation
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 可正确生成迁移。
This was referenced Oct 22, 2026
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 的修复留作后续。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
修复循环外键问题,关联 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)。改动
UserMixin三个审计字段(created_id/updated_id/deleted_id)移除指向sys_user的数据库级外键约束(ondelete=SET NULL已允许悬空,审计字段本就不需要强引用语义)。created_by/updated_by/deleted_by关系改用显式primaryjoin保持可查。dept_id移除use_alter=True(循环消除后不再需要)。效果
use_alter即可正确生成迁移。ruff check通过、pytest(PostgreSQL)74 passed、configure_mappers正常。关联