Prisma-client-js: Self-relations are not working as expected.

Created on 24 Dec 2019  路  8Comments  路  Source: prisma/prisma-client-js

Hi there,

I apologise in advance if this is my fault :)

My model has these self-relations

displacedBy WorksheetState?   @relation(name: "WorksheetStateDisplacementRelation")
displaces   WorksheetState?   @relation(name: "WorksheetStateDisplacementRelation")

When I run this code...

await photon.worksheetStates.update({
    where: {id: oldStateId},
    data: {displaces: {connect: {id: displacer.id}},}
  })

I get this in the db:
image

As you can see, the value goes into the displacedBy column. I was expecting it to go into a "displaces" column... but there is no such displaces column so I feel like I am missing something! I am also getting the same error with this code...

const displacer = await photon.${resource}States.create({
    data: {
      ...
      displaces: {connect: {id: oldState.id}}
   }
}

In this case, this new entity, displacer, gets a value in the "displacedBy" column, whereas what is essentially "meant" is for the oldState model to get displacer.id in displacedBy.

Thank you for any guidance if this is my fault, and if it isn't, thank you for looking into this!

kinquestion

Most helpful comment

All 8 comments

I am also struggling with a very basic query...

image

image

image

The entities that are returned include something that has a not null value for displaced, and excludes something that has a null value for displaced.

However, if I change the query to displaces: null, I do get the values I expect.

Thank you :)

Can you please share the whole datamodel or at least the WorksheetState model? That would we awesome.

Hey, thank you for looking into this, here it is:

model WorksheetState {
  id     String @id @default(cuid())
  platonic WorksheetPlatonic @relation(name: "PlatonicStateWorksheet")
  createdAt DateTime
  createdBy UserPlatonic @relation(name: "WorksheetStateCreatedBy")
  validFrom DateTime
  validUntil DateTime?
  displacedBy WorksheetState? @relation(name: "WorksheetStateDisplacementRelation")
  displaces WorksheetState? @relation(name: "WorksheetStateDisplacementRelation")
  title String
}

Hi,

I think this is just misconception in data modelling. You need two separate relations with four field out of which two will be used as a back relation by the engine.

Use the following datamodel for you use case:

model WorksheetState {
  id     String @id @default(cuid())
  platonic WorksheetPlatonic @relation(name: "PlatonicStateWorksheet")
  createdAt DateTime
  createdBy UserPlatonic @relation(name: "WorksheetStateCreatedBy")
  validFrom DateTime
  validUntil DateTime?
  displacedBy WorksheetState? @relation(name: "WorksheetStateDisplacementByRelation")
   displacedByBack WorksheetState? @relation(name: "WorksheetStateDisplacementByRelation")
  displaces WorksheetState? @relation(name: "WorksheetStateDisplacementRelation")
  title String
  displacesBack WorksheetState? @relation(name: "WorksheetStateDisplacementRelation")
  title String
}

Now this will create the required columns in the database:
image

I am assuming that Harshit's answer addresses this question and closing this one. If that is not the case, please comment on this issue and tag me.

Thanks

@divyenduz I'm seeing some unexpected behaviour when using self relations.

Given this schema:

model Person {
  id     String  @id @default(cuid())
  parent Person? @relation(name: "ParentChildRelation")
}

When I create 2 people: parent and child:

    const parent = await photon.people.create({
        data: {
          id: 'parent'
        }
      })

      const child = await photon.people.create({
        data: {
          id: 'child'
        }
      })

Then, update the child to have parent as its parent:

    await photon.people.update({
        where: {
          id: 'child'
        },
        data: {
          parent: {
            connect: {
              id: 'parent'
            }
          }
        }
      })

This is what I get:
image

I would have assumed that the row with the id of child to have parent in its parent column. Instead, it's the other way around. Is this supposed to be how this works?

Thanks

@stephensamra This looks like a bug, can you please create a new issue with a small reproduction?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samrith-s picture samrith-s  路  3Comments

divyenduz picture divyenduz  路  3Comments

timsuchanek picture timsuchanek  路  4Comments

FluorescentHallucinogen picture FluorescentHallucinogen  路  3Comments

divyenduz picture divyenduz  路  4Comments