通过界面新建的关联字段在加载时被静默丢弃 ——「添加字段」表单把畸形的 source 写进了字段 options
通过界面新建的关联字段在加载时被静默丢弃 ——「添加字段」表单把畸形的 source 写进了字段 options
环境
| -- | -- NocoBase 版本 | 2.2.19 相关包 | @nocobase/app、@nocobase/database、@nocobase/plugin-data-source-manager、@nocobase/client 全部为 2.2.19(依赖树中无版本错配) 安装方式 | my-nocobase-app 数据库 | PostgreSQL 18 APP_ENV | development 部署环境 | 本地 dev 服务,以及两台远程服务器(yarn start 构建运行)均复现问题概述
在界面上新建 belongsTo / belongsToMany 字段后(设置 → 数据源 → 某张表 → 配置字段 → 添加字段),字段被写进了 fields 表,但永远不会被加载进内存。结果是:
GET /api/collections:listMeta不返回该字段页面设计器的字段选择器里看不到它,导致它在任何地方都无法配置
已经引用了它的页面区块渲染为空白
而「设置 → 数据源 → 配置字段」这个页面仍然能看到该字段(因为它直接读 fields 表),这个不一致会让人误以为是页面配置或 ACL 的问题。
应用日志在每次集合重载时都会重复出现:
[warn] Source field "undefined" not found for field "f_xxx" at collection "t_xxx". Source collection: "t_xxx" module=database复现步骤
通过界面
设置 → 数据源 → Main → 任意一张表 → 配置字段
添加字段 → 选择 多对多 或 多对一
填写显示名称、选择目标数据表,提交
表单实际发出的请求(从 DevTools 抓取):
POST /api/collections/<collectionName>/fields:create{
"name": "f_z2u40pfv6my",
"type": "belongsToMany",
"uiSchema": { "x-component": "AssociationField", "x-component-props": { "multiple": true }, "title": "..." },
"interface": "m2m",
"foreignKey": "f_mc3fs3lms15",
"otherKey": "f_8dqv8d0hnql",
"source": "t_nxqgf6s3wao",
"sourceKey": "id",
"target": "t_3s4k8gtzqal",
"through": "t_fhm4ety9f9h",
"targetKey": "id"
}注意 "source": "t_nxqgf6s3wao" —— 它来自关联字段表单里的 「源数据表 / Source collection」 输入项。
最小复现(纯 API,不走界面)
curl -X POST http://localhost:13000/api/collections/<anyCollection>/fields:create \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <token>' \
-d '{
"name": "f_repro",
"type": "belongsToMany",
"interface": "m2m",
"source": "<anyCollection>",
"sourceKey": "id",
"target": "<someOtherCollection>",
"through": "t_repro_through",
"foreignKey": "f_repro_a",
"otherKey": "f_repro_b",
"targetKey": "id"
}'请求返回 200,fields 表里也确实建出了记录,且 options.source = "<anyCollection>"。
期望行为
source(由「源数据表」输入项写入)应当被当作"这条关联属于哪张表"的说明性元数据,或者根本就不该被持久化到 options 里。无论哪种,字段都应该正常加载,并且在页面设计器里可以选到。
实际行为
在下一次集合加载 / 重载时,该字段会被丢弃并打出警告;所有从内存读取的地方(collections:listMeta、页面设计器、关联选择器)都再也看不到它。重启无效 —— 每次启动都会被重新丢弃。
根因
options.source 这一个 key 有两种互相冲突的含义:
(1)界面写入的含义。 关联字段表单的「源数据表」输入项提交的是 source = "<集合名>",一个裸的表名。
(2)数据库层期望的含义。 @nocobase/database 的 Collection.setField() 把 options.source 当作字段继承来源来读,期望的格式是 "集合名.字段名":
// @nocobase/database — Collection.setField()
if (options.source) {
const [sourceCollectionName, sourceFieldName] = options.source.split('.');
const sourceCollection = this.db.collections.get(sourceCollectionName);
if (!sourceCollection) {
this.db.logger.warn(`source collection "${sourceCollectionName}" not found for field "${name}" at collection "${this.name}"`);
return null;
}
const sourceField = sourceCollection.fields.get(sourceFieldName);
if (!sourceField) {
this.db.logger.warn(
`Source field "${sourceFieldName}" not found for field "${name}" at collection "${this.name}". Source collection: "${sourceCollectionName}"`,
);
return null; // ← 字段在这里被静默丢弃
}
options = { ...omit(sourceField.options, ['name', 'primaryKey']), ...options };
}"t_nxqgf6s3wao".split('.') 只切出一段,所以 sourceFieldName 是 undefined,sourceCollection.fields.get(undefined) 返回 undefined,于是 setField() 提前返回,字段从未被注册。
两种含义就此撞车:表单存进去的是裸表名,加载器按 集合.字段 去解析,解析失败,字段被扔掉。
补充一点:这两侧的逻辑都不是新加的 —— options.source 这段代码在 @nocobase/database 的 2.2.0 ~ 2.2.19 中一直存在,「源数据表」的文案在 plugin-data-source-manager / client 的语言包中 2.2.11 ~ 2.2.19 也一直存在。
证据
1. 应用日志(storage/logs/main/system_*.log),每次重载都重复出现:
2026-09-28 10:25:42 [warn] Source field "undefined" not found for field "f_sia34y0akp8" at collection "t_nxqgf6s3wao". Source collection: "t_nxqgf6s3wao" module=database
2026-09-28 18:14:18 [warn] Source field "undefined" not found for field "f_yyrfyvgdro3" at collection "t_ookjarlxzd2". Source collection: "t_ookjarlxzd2" module=database2. 把 fields 表与 collections:listMeta 在全部 63 个集合上做 diff:
凡是带 options.source 的字段,都不在 listMeta 里;凡是不带的,都在。除此之外找不到任何其他相关性 —— interface 相同、target 相同、通过表也相同,有 source 的丢、没 source 的不丢。
带 source(在 listMeta 中缺失):13 个字段
不带 source(在 listMeta 中存在):其余全部3. 请求体(见上方复现)证明界面确实发出了 source。
临时规避
把畸形值清掉,然后让集合重新加载:
update fields
set options = (options::jsonb - 'source')::json
where options->>'source' is not null;然后强制重载 —— 重启应用;或者不重启,对每张受影响的表各触发一次集合重载:
POST /api/collections:update?filterByTk=<collectionName>
{ "title": "<该表现有的标题>" }之后字段就会重新出现在 listMeta 中,也能正常配置了。注意 belongsToMany 不需要补建任何列;而从未成功加载过的 belongsTo 字段,其物理外键列是缺失的,会在重载过程中被自动补建。
影响
一种静默、且极难定位的数据模型损坏:
字段会一直堆在
fields表里,而且在表结构页面上看得到,所以在有人真正去用它之前,一切看起来都是正常的。它们在页面设计器、关联选择器以及所有依赖
listMeta的界面里都不可见。已经引用了它们的页面会静默渲染成空白。
因为这个值只在创建时写入,一个项目可以正常运行很久,然后突然"丢掉"某个时间点之后新建的字段 —— 而且没有任何明显的触发点。
补充说明
只有持有方会被写入。表单建出的
belongsToMany带source,而在目标表上自动生成的反向字段不带。同样方式建出的hasMany/o2m字段也不带。我无法确定为什么这个问题看起来不是所有安装都会中招。依赖树里所有包版本一致(2.2.19),本地
@nocobase/database的构建产物与 npm 上发布的 tarball 逐字节一致,表单输入项的 i18n key 也是官方的Source collection。如果本应在setField()之前有一个环节会剥掉这个值,那它在这套环境里没有生效。建议的修复方向:要么不要再把表单的「源数据表」持久化进
options.source(它看起来只是展示用),要么让setField()只在值里确实含.时才把它当作继承来源 —— 目前一个裸集合名会导致字段被丢弃,而不是优雅降级。
Source: nocobase/nocobase