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

时间:2020-02-26 17:31:12

标签: graphql hasura

我有2个表import cv2, os import numpy as np waves = {} # Opens the Video file cap=cv2.VideoCapture('8atpo.mp4') num_of_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) #1586 def open_frame(frame_no): while(cap.isOpened()): cap.set(1,frame_no) ret, frame = cap.read() if ret == False: break # frame consists of 1078 arrays. One for each height pixel. # each array consists of 1360 arrays. One for each width pixel. # each array consits of 3 values. RGB?? return frame def read_box(frame_no, nx1=0,nx2=2016,ny1=0,ny2=1600): cframe = open_frame(frame_no) # two slowest parts pframe = open_frame(frame_no - 1) # two slowest parts diff_frame = cframe[nx1:nx2, ny1:ny2] - pframe[nx1:nx2, ny1:ny2] return np.mean(diff_frame) stage ("Notify Developers of Success"){ env.ForEmailPlugin = env.WORKSPACE emailext attachmentsPattern: 'Checkmarx\\Reports\\*.pdf', to: "${primaryOwnerEmail},${secondaryOwners}", subject: "${env.JOB_NAME} - (${env.BUILD_NUMBER}) Finished Successfuly!", body: "Check console output at ${env.BUILD_URL} to view the results" }

users具有列postusers,该列包含格式为id的数组-其中post是表中的[1, 2, 3, 4, 5] 1, 2, 3, 4, 5

在表id中,以下列postposts

id

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

text

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

在hasura中建立了数组关系

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

接下来,我发出了以下请求

users

我希望收到这样的数据作为响应:

posts

但是有了这样的请求,我得到了跟踪。错误:

{
  users_test {
    postz {
      id
    }
  }
}

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

1 个答案:

答案 0 :(得分:3)

一些建议:

  1. 据我所知,您的查询中有一些错别字。试试:
{
  users {
    id
    posts {
      text
    }
  }
}
  1. 您不需要post表上的users列。您只需要user_id表上的posts列,以及使用posts和{{}从users表到user_id表的外键约束表的1}}列。在此处查看文档:

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. 如果由于某种原因必须拥有id数组列,则可以使用计算字段在json数组和另一个表的ID之间创建“关系”。

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

您的功能将:

  • 进入json数组列

  • 提取ID的

  • 从表中id为id的表中返回* *

示例:

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

位于https://jsonb-relationships-hasura.herokuapp.com/console/data/schema/public/tables/authors/modify

的计算字段定义

运行以下查询:

post
# Get list of articles for each author
query {
  authors {
    id
    name
    articles
  }
}