关系 Relations

Directus 中关系集合的 REST 和 GraphQL API 文档。哪些数据与哪些其他数据相关联。 允许您将作者分配给文章,将产品分配给销售,以及您能想到的任何其他结构。

关系对象

collection string
集合的名称。这与数据库中的表名匹配。

field string
包含相关主键的字段的名称。这与数据库中的列名匹配。

related_collection string
相关集合的名称。这与数据库中的表名匹配。

元数据

Directus 元数据。 用于启用非数据库关系类型

id integer
directus_relations 中元数据行的主键。

many_collection string
集合的名称。匹配顶级 collection 字段。

many_field string
字段的名称。匹配顶级 field 字段。

one_collection string
相关集合的名称。匹配顶级 related_collection 字段。

one_field string
关系另一侧的一对多字段的名称。

one_allowed_collections string
允许在多对任意上下文中使用哪些集合。

one_collection_field string
在多对任意上下文中保存集合名称的字段。

one_deselect_action nullify | delete
是否取消或删除相关的一对多记录。

sort_field string
什么字段用于保存排序字段。

junction_field string
什么字段在多对多 (O2M-M2O) 上下文中连接两个关系。

结构

“原始”数据库信息。 根据使用的数据库供应商,可能会返回不同的信息。 以下适用于所有驱动程序。

table string
表名。

column string
列名称。

foreign_key_table string
相关表名。

foreign_key_column string
相关列名。

constraint_name string
外键约束的名称。

on_update string
外键约束的更新触发器。

on_delete string
删除外键约束的触发器。

json
{
  "collection": "about_us",
  "field": "logo",
  "related_collection": "directus_files",
  "schema": {
    "table": "about_us",
    "column": "logo",
    "foreign_key_table": "directus_files",
    "foreign_key_column": "id",
    "constraint_name": "about_us_logo_foreign",
    "on_update": "NO ACTION",
    "on_delete": "SET NULL"
  },
  "meta": {
    "id": 1,
    "junction_field": null,
    "many_collection": "about_us",
    "many_field": "logo",
    "one_allowed_collections": null,
    "one_collection": "directus_files",
    "one_collection_field": null,
    "one_deselect_action": "nullify",
    "one_field": null,
    "sort_field": null
  }
}

列出关系

列出 Directus 中存在的所有关系。

权限
此端点中返回的数据将根据用户的权限进行过滤。 例如,应用于当前用户无权访问的集合的关系将被删除。

查询参数

不支持任何查询参数。

返回

关系对象的数组。 如果没有可用的项目,数据将是一个空数组。

REST API

GET /relations

GraphQL

POST /graphql/system
graphql
type Query {
 relations: [directus_relations]
}
示例
graphql
query {
 relations {
  collection
  field
 }
}

列出集合中的关系

列出给定集合中存在的所有关系。

权限
此端点中返回的数据将根据用户的权限进行过滤。 例如,应用于当前用户无权访问的集合的关系将被删除。

查询参数

不支持任何查询参数。

返回

关系对象的数组。 如果没有可用的项目,数据将是一个空数组。

REST API

GET /relations/:collection
示例
GET /relations/articles

GraphQL

POST /graphql/system
graphql
type Query {
 relations_in_collection(collection: String!): [directus_relations]
}
示例
graphql
query {
 relations_in_collection(collection: "articles") {
  collection
  field
 }
}

检索关系

按集合/字段名称列出现有关系。

查询参数

不支持任何查询参数。

返回

返回请求的 关系对象

REST API

GET /relations/:collection/:field
示例
GET /relations/articles/featured_image

GraphQL

POST /graphql/system
graphql
type Query {
 relations_by_name(collection: String!, field: String!): directus_relations
}
示例
graphql
query {
 relations_by_name(collection: "articles", field: "featured_image") {
  collection
  field
  related_collection
 }
}

创建关系

创建一个新的关系。

查询参数

不支持任何查询参数。

请求正文

部分关系对象

返回

返回已创建关系的 relation object

REST API

POST /relations
示例
json
// POST /relations

{
  "collection": "articles",
  "field": "featured_image",
  "related_collection": "directus_files"
}

GraphQL

POST /graphql/system
graphql
type Mutation {
 create_relations_item(data: create_directus_relations_input!): directus_relations
}
示例
graphql
mutation {
 create_relations_item(
  data: { collection: "articles", field: "featured_image", related_collection: "directus_files" }
 ) {
  collection
  field
  related_collection
 }
}

更新关系

更新现有关系。

查询参数

不支持任何查询参数。

请求正文

部分关系对象

返回

返回已创建关系的 关系对象

REST API

PATCH /relations/:collection/:field
示例
json
// PATCH /relations/articles/author

{
  "meta": {
    "one_field": "articles"
  }
}

GraphQL

POST /graphql/system
graphql
type Mutation {
 update_relations_item(collection: String!, field: String!, data: update_directus_relations_input!): directus_relations
}
示例
graphql
mutation {
 update_relations_item(collection: "articles", field: "author", data: { meta: { one_field: "articles" } }) {
  collection
  field
  related_collection
 }
}

删除关系

删除现有关系。

Returns

Empty body.

REST API

DELETE /relations/:collection/:field
示例
DELETE /relations/articles/author

GraphQL

POST /graphql/system
graphql
type Mutation {
 delete_relations_item(collection: String!, field: String!): delete_one
}
示例
graphql
mutation {
 delete_relations_item(collection: "articles", field: "author") {
  collection
  field
 }
}