Relation createdAt

prisma

#1

Hi there.

I have a relation between two table:

type TableA {
  id: ID! @id
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt
  myRelationField: [TableB!]! @relation(link: TABLE, name: "TableBrelation")
  ...more fields
}

type TableB {
  id: ID! @id
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt
  ...more fields
}

Then, I’m able to do queries as:

query {
   tableA {
     id
     myRelationField {
       id
     } 
  }
}

The issue for me is, the related data is ordered by that table createdAt field.
What I need is to get the data ordered by the date they were connected to the relation

Is there any way to do this?

Cheers.


#2

You will need to use orderBy:

query {
   tableA {
     id
     myRelationField(orderBy: createdAt_DESC) { # or createdAt_ASC
       id
     } 
  }
}

#3

Thanks for your reply.

But this will retrieve the data ordered by their createdAt in their table, right?

That’s the issue that I described in my original post:

The issue for me is, the related data is ordered by that table createdAt field.
What I need is to get the data ordered by the date they were connected to the relation


#4

You will need to store that value. We don’t store the time at which you connect a relation with the parent


#5

Thanks again for your replay @pantharshit00
How can I create/store a custom value in a relation??


#6

Make a field connectedAt: DateTime! and add it at the time of creation of the record manually


#7

@pantharshit00 I really appreciate your help, but I’m afraid you don’t get the issue I’m having…

How can I add a field in a relation??
myRelationField: [TableB!]! @relation(link: TABLE, name: "TableBrelation")
Am I missing something?