Skip to main content

Naming conflict

You're probably here because you just received an error like:

你之所以会看到这个页面,多半是因为遇到了类似下面的报错:

Error: A naming conflict has occurred - two entities have tried to define the same key 'task'.
The first entity was:
Adding 'task' attribute field to GraphQL type 'Subtask' (representing PgCodec 'subtask')
The second entity was:
Adding field to GraphQL type 'Subtask' for singular relation 'taskByMyTask'
Details: https://err.red/pnc

This happens when PostGraphile tries to add an entry to an object, where that object already has an entry with that name. There is a very large number of circumstances under which this can happen since the build.extend method is used to avoid conflicts throughout the PostGraphile schema building process: when creating codecs, resources or relations; when adding attributes; when defining GraphQL fields, arguments or enum values; and much more.

当 PostGraphile 试图向某个对象添加成员,而该对象中已存在同名成员时,就会抛出此错误。由于整个 PostGraphile schema 构建流程中广泛使用 build.extend 方法来规避覆盖冲突——包括创建 codec、resource 或 relation,添加属性,定义 GraphQL 字段、参数或枚举值等诸多环节——触发这种冲突的场景非常多。

Fortunately we try and give you more details about how the error has come about by detailing the key ('task' above) that has conflicted, and where the first and second conflicting entities originated from.

好在报错信息中已尽量详细地标明了发生冲突的键名(例如上例中的 'task'),以及先后两个冲突实体各自的来源。

When reading about the entities, you should keep in mind that codec/PgCodec, resource/PgResource, and relation/PgCodecRelation relate to the concepts described in the registry documentation.

在阅读报错中的实体说明时,请留意其中提到的 codec/PgCodec、resource/PgResource 以及 relation/PgCodecRelation,它们对应的正是 registry 文档 中介绍的核心概念。

A common situation where this would occur is where you have two different things (e.g. a column and a relationship) that are trying to be assigned to the same field name in the GraphQL schema. This is particularly common when using @graphile/simplify-inflection because it deliberately shortens the names of relations for better ergonomics (e.g. userByAuthorId might become simply author), at the risk of causing more conflicts - this is the main reason this preset isn't enabled by default.

一种常见的情况是,两个不同的数据库对象(例如一个表列和一个关联关系)在 GraphQL schema 中被推导成了同一个字段名。使用 @graphile/simplify-inflection 时尤其容易出现这类问题:该 preset 为了提供更友好的 API 体验,有意缩短了 relation 的字段名(例如将 userByAuthorId 简化为 author),但代价是增加了命名冲突的概率——这也是该 preset 默认未启用的主要原因。

Another common situation is where you have two things of the same type (e.g. two columns) whose names are made the same after inflection (e.g. columns named thing_2name, thing2_name, thing2name and thing2Name would all transform to a GraphQL field called thing2Name in the default inflectors).

另一种常见情况是,两个同类型的对象(例如两列)在经过命名推导(inflection)后变成了相同的名称。比如,名为 thing_2name、thing2_name、thing2name 和 thing2Name 的列,在默认 inflector 下都会被转换为名为 thing2Name 的 GraphQL 字段。

To resolve the issue, you should either:

要解决此问题,你可以:

  1. use a smart tag to assign a different name or field name to one or both of the entities, or

    使用 smart tag 为其中一方或双方指定不同的名称或字段名;或

  2. use a custom inflector to change the way the names are generated for the entities, or

    使用自定义 inflector 调整实体名称的生成规则;或

  3. do something else suitable for the situation.

    根据具体场景采取其他合适的处理方式。

If you really are not sure what to do, please come ask us in the #help-and-support channel on our Discord at https://discord.gg/graphile - we might even add the solution to this page for future users!

如果仍然不确定如何处理,欢迎加入我们的 Discord(https://discord.gg/graphile)并在 #help-and-support 频道提问——我们甚至可能会把你的解决方案补充到本页面,帮助更多后续用户!