如何在 Hasura + Postgres 中创建 `json` 列和 `int` (id) 列之间的关系?

How can I create a relationship between `json` column and a `int` (id) column in Hasura + Postgres?

我有 2 tables userspost

Table users 有列 idpost,列包含 [1, 2, 3, 4, 5] 形式的数组 - 其中 1, 2, 3, 4, 5id 在 table post

在tableposts下面的栏目idtext

Table users:

https://i.stack.imgur.com/ywdS7.png

Table posts:

https://i.stack.imgur.com/IBdpb.png

在hasura中建立了一个数组关系

https://i.stack.imgur.com/311sd.png

接下来我提出了以下要求

{
  users_test {
    postz {
      id
    }
  }
}

我想收到这样的数据作为回应:

    postz: [
       {
         text: 'qwe'
       },
       {
         text: 'sdf'
       }
    ]

但是这样的要求,我得到了踪迹。错误:

{
  "errors": [
    {
      "extensions": {
        "internal": {
          "statement": "SELECT  coalesce(json_agg(\"root\" ), '[]' ) AS \"root\" FROM  (SELECT  row_to_json((SELECT  \"_5_e\"  FROM  (SELECT  \"_4_root.ar.root.postz\".\"postz\" AS \"postz\"       ) AS \"_5_e\"      ) ) AS \"root\" FROM  (SELECT  *  FROM \"public\".\"users_test\"  WHERE ('true')     ) AS \"_0_root.base\" LEFT OUTER JOIN LATERAL (SELECT  coalesce(json_agg(\"postz\" ), '[]' ) AS \"postz\" FROM  (SELECT  row_to_json((SELECT  \"_2_e\"  FROM  (SELECT  \"_1_root.ar.root.postz.base\".\"id\" AS \"id\"       ) AS \"_2_e\"      ) ) AS \"postz\" FROM  (SELECT  *  FROM \"public\".\"posts\"  WHERE ((\"_0_root.base\".\"post\") = (\"id\"))     ) AS \"_1_root.ar.root.postz.base\"      ) AS \"_3_root.ar.root.postz\"      ) AS \"_4_root.ar.root.postz\" ON ('true')      ) AS \"_6_root\"      ",
          "prepared": true,
          "error": {
            "exec_status": "FatalError",
            "hint": "No operator matches the given name and argument type(s). You might need to add explicit type casts.",
            "message": "operator does not exist: json = integer",
            "status_code": "42883",
            "description": null
          },
          "arguments": [
            "(Oid 114,Just (\"{\\"x-hasura-role\\":\\"admin\\"}\",Binary))"
          ]
        },
        "path": "$",
        "code": "unexpected"
      },
      "message": "postgres query error"
    }
  ]
}

我做错了什么,我该如何解决?

几点建议:

  1. 据我所知,您的查询中有一些拼写错误。尝试:
{
  users {
    id
    posts {
      text
    }
  }
}
  1. 您不需要 users table 上的 post 列。您只需要 posts table 上的 user_id 列,以及从 posts table 到 users [=76] 的外键约束=] 分别使用 table 的 user_idid 列。在此处查看文档:

https://docs.hasura.io/1.0/graphql/manual/schema/relationships/create.html#step-3-create-an-array-relationship

https://docs.hasura.io/1.0/graphql/manual/schema/relationships/database-modelling/one-to-many.html

  1. 如果出于某种原因必须使用 post 数组列,您可以使用计算字段在 json 数组和另一个 table 数组之间创建一个 "relationship"的编号。

https://docs.hasura.io/1.0/graphql/manual/schema/computed-fields.html#table-computed-fields

您的函数将:

  • 取入json数组列

  • 提取id的

  • Return select * 来自 table where id in id's

示例:

https://jsonb-relationships-hasura.herokuapp.com/console/api-explorer

计算字段定义位于:https://jsonb-relationships-hasura.herokuapp.com/console/data/schema/public/tables/authors/modify

运行 这些查询:

# Get list of articles for each author
query {
  authors {
    id
    name
    articles
  }
}
# Get actual articles for each author
query {
  authors {
    id
    name
    owned_articles {
      id
      title
    }
  }
}