Can't get one-to-many self-relations to work properly?

framework
prisma

#1

Here is my model:

type ServiceRequest {
  id: ID! @unique
  description:String!
  derivedFromRequest: ServiceRequest
    @relation(
      name: "DerivedServiceRequestsOnServiceRequest"
      onDelete: SET_NULL
    )

  derivedRequests: [ServiceRequest!]!
    @relation(
      name: "DerivedServiceRequestsOnServiceRequest"
      onDelete: SET_NULL
    )
}

And here is what I did to create the relation

1 - First create parent with no relation

# mutation
createServiceRequest( data: { description: "root parent" }) {
  id
  description
}



# here is the result
{
  "data": {
    "createServiceRequest": {
      "id": "cjv4w3fdde8c70b22jz2w1znd",
      "description": "root parent"
    }
  }
}

2 -Then try to update the parent to connect it with a new child

# mutation
updateServiceRequest(
    # root parent id
    where: { id: "cjv4w3fdde8c70b22jz2w1znd" }
    data: {
      derivedRequests: {
        create: { description: "derived request" }
      }
    }
  )
{
    id
    derivedRequests {
      id
      description
      derivedFromRequest {
        id
        description
      }
    }
}


# Here is the result
{
  "data": {
    "updateServiceRequest": {
      "id": "cjv4w3fdde8c70b22jz2w1znd",
      "description": "root parent",
      "derivedRequests": []
    }
  }
}

The issue is the desired relationship is not being made.

I expected the following result for the second mutation

# expected result

{
  "data": {
    "updateServiceRequest": {
      # new child id
      "id": "cjv4w3fdde8c70b22jz2w1znd",
      "description": "root parent",
      "derivedRequests": [
        {
          "id":"cjv4w6o801s620b95idhi15r2",
          "description":"derived request",
          "derivedFromRequest":{
             # parent id
            "id" : "cjv4w3fdde8c70b22jz2w1znd"
          }
        }
      ]
    }
  }
}

Furthermore
When I try to query all service requests I have noticed that the new child entity has the correct link to its parent

{
  "id": "cjv4w6o801s620b95idhi15r2",
  "description": "derived request",
  "derivedRequests": [],
  "derivedFromRequest": {
    "id": "cjv4w3fdde8c70b22jz2w1znd",
    "description": "root parent"
  }
}

Hence the only problem is on the one side of the relation, i.e the parent not having a connection to its children

I would really appreciate getting any feedback.
Thanks!


#2

I got the same error. Did you find any fix @m3hari ?


#3

Please open a GitHub issue here about this: https://github.com/prisma/prisma/issues/new?template=bug_report.md


#4

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.