Skip to main content

Two resources conflicted

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

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

Error: Attempted to add a second resource named 'foo':
First represented a function accepting 0 parameters and returning SQL type '"bool"'.
Second represents a table/view/etc called '"public"."foo"'.
Details: https://err.red/p2rc

This happens when PostGraphile is building the resources for your schema, and the inflection rules tell it to assign them both the same name

该错误发生在 PostGraphile 为你的 schema 构建 resource 时,命名推导(inflection)规则将两个不同的实体推导出了相同的名称。

Remember: a resource represents somewhere that you can pull data from in your database, for example a table or a function.

请记住:resource 代表数据库中可以读取数据的数据源,例如一张表或一个函数。

This might happen if:

以下情况可能导致该冲突:

  • you have a table and a function with the same (or similar) names

    数据库中存在同名(或名称相近)的表和函数;

  • you have tables with the same (or similar) names in multiple schemas

    多个数据库 schema 中存在同名(或名称相近)的表;

  • you have "interesting" inflection rules 😉

    自定义的 inflection 规则比较“特别” 😉

The error itself should hopefully hint at which entities are clashing. To resolve the issue, you should either:

报错信息本身通常会提示具体是哪两个实体发生了冲突。要解决这一问题,你可以采取以下任一方式:

  1. rename one of the database entities, or

    重命名其中一个数据库实体;或

  2. use a smart tag to assign a different name to one of the resources, or

    使用 smart tag 为其中一个 resource 指定不同的名称;或

  3. use a custom inflector to give one of the entities a different name.

    编写自定义 inflector,为其中一个实体生成不同的名称。

Generally the smart tag is the recommended approach for one-off issues, however if this is a systemic issue (for example you use a lot of schemas, and the schemas frequently have a lot of table names in common) then it may be wise to change your inflectors to make the resource names more unique - for example factoring the schema name into the name of the resource.

对于偶发的单点冲突,通常推荐使用 smart tag 来解决;但如果冲突具有系统性(例如你使用了多个数据库 schema,且各 schema 之间经常出现大量同名表),那么修改 inflector 来提高 resource 名称的区分度会更明智——比如把数据库 schema 名称也拼接进 resource 名称中。