Hi there, could anyone help me out with how should...
# orm-help
e
Hi there, could anyone help me out with how should I approach this? I'm trying to replace (disconnect - connect) a relation of
MessageRead
to `Message`:
Copy code
model Message {
  id             String               @id @default(uuid())
  content        String
  createdAt      DateTime             @default(now()) @map("created_at")
  sender         User                 @relation(fields: [senderId], references: [id])
  senderId       Int                  @map("sender_id")
  read           MessageRead?

  @@map("message")
}

model MessageRead {
  id            String                        @id @default(uuid())
  seenAt        DateTime                      @default(now()) @map("seen_at")
  seenMessage   Message                       @relation(fields: [seenMessageId], references: [id])
  seenMessageId String                        @map("seen_message_id")
  user          User                          @relation(fields: [userId], references: [id])
  userId        Int                           @map("user_id")

  @@map("message_read")
}
MessageRead
keeps track of the
User
latest read message. I'm currently trying to figure out why this doesn't update the connection of
MessageRead
`seenMessage`:
Copy code
await this.prisma.messageRead.upsert({
  where: {
	id
  },
  update: {
	seenAt: new Date(),
	seenMessage: {
	  connect: {
		id: message.id,
	  },
	},
  },
  create: {
	user: {
	  connect: {
		id: user.id,
	  },
	},
	seenMessage: {
	  connect: {
		id: message.id,
	  },
	},
  },
});
The error code I get:
Copy code
- Invalid `prisma.messageRead.upsert()` invocation:
The change you are trying to make would violate the required relation 'MessageToMessageRead' between the `Message` and `MessageRead` models.
r
I created two users with this model and it works fine:
Copy code
model User {
  id           Int           @id @default(autoincrement())
  name         String
  messages     Message[]
  messagesRead MessageRead[]
}

model Message {
  id        String       @id @default(uuid())
  content   String
  createdAt DateTime     @default(now()) @map("created_at")
  sender    User         @relation(fields: [senderId], references: [id])
  senderId  Int          @map("sender_id")
  read      MessageRead?

  @@map("message")
}

model MessageRead {
  id            String   @id @default(uuid())
  seenAt        DateTime @default(now()) @map("seen_at")
  seenMessage   Message  @relation(fields: [seenMessageId], references: [id])
  seenMessageId String   @map("seen_message_id")
  user          User     @relation(fields: [userId], references: [id])
  userId        Int      @map("user_id")

  @@map("message_read")
}
And the query:
Copy code
let u1 = await prisma.user.create({ data: { name: 'user 1' } })
  let u2 = await prisma.user.create({
    data: { name: 'user 2', messages: { create: { content: 'message 1' } } },
    include: { messages: true },
  })

  await prisma.messageRead.upsert({
    where: {
      id: 'f654306a-01b6-4201-94fc-e09a2ec11c83',
    },
    update: {
      seenAt: new Date(),
      seenMessage: {
        connect: {
          id: u2.messages[0].id,
        },
      },
    },
    create: {
      user: {
        connect: {
          id: u1.id,
        },
      },
      seenMessage: {
        connect: {
          id: u2.messages[0].id,
        },
      },
    },
  })
Could you check if you are not passing an
undefined
id to
connect
?
e
checking rn
damn, i'm stupid
it wans't undefined but I was trying to connect it with the same message
👍 1
thank you very much @Ryan
💯 1