Handling data @relation

prisma

#1

Have a question about relations that would be thankful if anyone answer. Assume we have these two types (connected as you can see):

type Customer {
  customer_id: Int
  premium_user: Boolean @default(value: "false")
  shop: String
  complaints: [Complaint!]! @relation(name: "ComplaintsByCustomer" onDelete: CASCADE)
}
type Complaint {
  complaint_id: Int
  shop: String
  orderitem_id: Int
  description: String
  current_customer: Customer @relation(name: "ComplaintsByCustomer")
}

Now i wanna query all the complaints that the user has based on the “shop” field and “customer_id”. so I wanna: say get complaints where “shop” is THIS and customer_id is THIS. Here’s the query I currently have which outputs the data from Customer table and an empty array for Complaints.

customers(where: {shop: "dare"}) {
	customer_id
	email
	shop
	last_name
	complaints {
		shop
		order_id
		description
	}

}

Thanks


#2

It looks like the queried customers are not connected to any complaints yet.


#3

we have “shop” field in both tables and at least one item with this value. is there anything else i should do?


#4

It looks like your data doesn’t fulfill the filter conditions that you are querying.

If you believe this to be wrong, can you share a live endpoint, a query, the expected response and the actual response? :slight_smile:


#5

Sure. Will share it with you in the livechat. Thanks


#6

Please share it here for everybody :slight_smile:

You haven’t actually described yet what the expected result is, can you elaborate?


#7

I just set up a project with the datamodel you shared, and all queries work as expected. Here’s a step-by-step description of what exactly I did:

  1. Deploy service with the following datamodel

    type Customer {
      customer_id: Int
      premium_user: Boolean @default(value: "false")
      shop: String
      complaints: [Complaint!]! @relation(name: "ComplaintsByCustomer" onDelete: CASCADE)
    }
    type Complaint {
      complaint_id: Int
      shop: String
      orderitem_id: Int
      description: String
      current_customer: Customer @relation(name: "ComplaintsByCustomer")
    }
    
  2. Create test data with the following mutation

    mutation a {
      createCustomer(
        data: {
          customer_id: 1
          premium_user: false
          shop: "abc"
          complaints: {
            create: {
              complaint_id: 2
              shop: "abc"
              orderitem_id: 3
              description: "some complaint"
            }
          }
        }
      ) {
        customer_id
      }
    }
    
  3. Query the relational data in both directions with the following two queries

    query b {
      customers(where: { shop: "abc" }) {
        customer_id
        shop
        complaints {
          shop
          description
        }
      }
      
      complaints(where: { shop: "abc" }) {
        complaint_id
        shop
        current_customer {
          shop
        }
      }
    }
    

    This query returns the expected result:

    {
      "data": {
        "customers": [
          {
            "customer_id": 1,
            "shop": "abc",
            "complaints": [
              {
                "shop": "abc",
                "description": "some complaint"
              }
            ]
          }
        ],
        "complaints": [
          {
            "complaint_id": 2,
            "shop": "abc",
            "current_customer": {
              "shop": "abc"
            }
          }
        ]
      }
    }
    

You can test this yourself at this public endpoint.


#8

I just realized where this issue comes from. Prisma won’t make relation between those table rows that are generated in any other way but using prisma itself!
It immediately creates relation between them when i create using playground and nothing happens when i update database manually.
Is there any way to fix this?


#9

So is there any way to fix this?


#10

Yes, that’s correct. If you update your data bypassing Prisma, Prisma will not intervene.
The fix is to not bypass Prisma, or ensure that relational data is consistent manually.


#11

I seem. So assuming we have data not generated by prisma, how can I manually tell prisma that they’re related to each other?
I tried to manipulate the prisma relations table to do that but seems like that’s not the right way to do it.


#12

That’s exactly the right way to do it.

You can run the equivalent mutation through the Prisma API in a new empty test project and see what data is written to the database to see what you need to do manually :slight_smile: