Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expected parent IDs to be set when ordering by parent ID. #23912

Open
DarkWorldCoder opened this issue Apr 21, 2024 · 2 comments
Open

Expected parent IDs to be set when ordering by parent ID. #23912

DarkWorldCoder opened this issue Apr 21, 2024 · 2 comments
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. team/client Issue for team Client. tech/engines Issue for tech Engines.

Comments

@DarkWorldCoder
Copy link

Hi Prisma Team! My Prisma Client just crashed. This is the report:

Versions

Name Version
Node v20.5.1
OS debian-openssl-3.0.x
Prisma Client 4.14.1
Query Engine d9a4c5988f480fa576d43970d5a23641aa77bc9c
Database mongodb

Logs

prisma:tryLoadEnv Environment variables loaded from /home/ayushniroula/Desktop/Projects/bookmart/backend/.env
prisma:client checkPlatformCaching:postinstall false
prisma:client checkPlatformCaching:ciName 
prisma:tryLoadEnv Environment variables loaded from /home/ayushniroula/Desktop/Projects/bookmart/backend/.env
prisma:client dirname /home/ayushniroula/Desktop/Projects/bookmart/backend/node_modules/.prisma/client
prisma:client relativePath ../../../prisma
prisma:client cwd /home/ayushniroula/Desktop/Projects/bookmart/backend/prisma
prisma:client protocol graphql
prisma:client clientVersion 4.14.1
prisma:client clientEngineType library
prisma:client:libraryEngine internalSetup
prisma:get-platform Found distro info:
{
  "targetDistro": "debian",
  "familyDistro": "debian",
  "originalDistro": "ubuntu"
}
prisma:get-platform Trying platform-specific paths for "debian" (and "ubuntu")
prisma:get-platform Found libssl.so file using platform-specific paths: libssl.so.3
prisma:get-platform The parsed libssl version is: 3.0.x
prisma:client:libraryEngine:loader Searching for Query Engine Library in /home/ayushniroula/Desktop/Projects/bookmart/backend/node_modules/.prisma/client
prisma:client:libraryEngine:loader loadEngine using /home/ayushniroula/Desktop/Projects/bookmart/backend/node_modules/.prisma/client/libquery_engine-debian-openssl-3.0.x.so.node
prisma:client:libraryEngine library starting
prisma:client:libraryEngine library started
prisma:client:libraryEngine sending request, this.libraryStarted: true
prisma:client:libraryEngine sending request, this.libraryStarted: true
prisma:client:libraryEngine sending request, this.libraryStarted: true
prisma:client:libraryEngine sending request, this.libraryStarted: true

Client Snippet

// PLEASE FILL YOUR CODE SNIPPET HERE
   sendMessage:async(
            _:any,
            args:SendMessageArguments,
            context:GraphQLContext
        ):Promise<boolean>=>{
            const {session,prisma,pubsub} = context;
            if(!session?.user.id){
                throw new ApolloError("User not authenticated to send message")
            }
            const {id:userId} = session.user;
            const {id:messageId,senderId,conversationId,body} = args 

            try{
                const newMessage = await prisma.message.create({
                    data:{
                        id:messageId,
                        senderId,
                        conversationId,
                        body
                    },
                    include:messagePopulated
                })

                // todo cache this in production if needed 

                const participant = await prisma.conversationParticipant.findFirst({
                    where:{
                        userId,
                        conversationId
                    }
                })

                if(!participant){
                    throw new ApolloError("User not allowed to send message")
                }
                const {id:participantId} = participant
                
                // updating conversation
                const conversationFind = await prisma.conversation.findUnique({
                    where:{
                        id:conversationId
                    }
                })
                const conversation = await prisma.conversation.update({
                    where:{
                        id:conversationId
                    },
                    data:{
                        latestMessageId:newMessage.id,
                        participants:{
                            update:{
                                where:{
                                    id:participantId
                                },
                                data:{
                                    hasSeenLatestMessage:true,
                                }
                            },
                            updateMany:{
                                where:{
                                    NOT:{
                                        userId
                                    }
                                },
                                data:{
                                    hasSeenLatestMessage:false
                                }
                            }
                        },
                        isActive:conversationFind?.isActive === false ? true : true
                    },
                    include:conversationPopulated
                })
                if(!conversationFind?.isActive){
                    pubsub.publish(EventTypes.CONVERSATION_CREATED,{
                        conversationCreated:{
                            ...conversation
                        }
                    })  
                }
                    pubsub.publish(EventTypes.CONVERSATION_UPDATED,{
                        conversationUpdated:{
                            conversation
                        }
                    })
                // console.log(pubsub)
                pubsub.publish(EventTypes.MESSAGE_SENT,{
                    messageSent:newMessage
                })
                return true

            }catch(error:any){
                console.log(error)
                throw new ApolloError(error?.message)
            }
        }

Schema

// PLEASE ADD YOUR SCHEMA HERE IF POSSIBLE


model Conversation {
  id              String    @id @default(auto()) @map("_id") @db.ObjectId
  participants    ConversationParticipant[]
  messages        Message[] @relation("conversationMessages")
  latestMessage   Message?   @relation(name: "latestConversationMessage", fields: [latestMessageId], references: [id], onUpdate: NoAction, onDelete: NoAction)
  latestMessageId String?    @unique @db.ObjectId
  createdAt       DateTime  @default(now())
  updatedAt       DateTime  @updatedAt
  isDeleted       Boolean   @default(false)
  isArchived      Boolean   @default(false)
  isActive       Boolean   @default(true)
}

model ConversationParticipant {
  id              String    @id @default(auto()) @map("_id") @db.ObjectId
  user            User      @relation(fields: [userId], references: [id])
  userId          String    @db.ObjectId
  conversation    Conversation @relation(fields: [conversationId], references: [id])
  conversationId  String  @db.ObjectId
  hasSeenLatestMessage Boolean
}
enum MessageType {
  TEXT
  IMAGE
  VIDEO
  AUDIO
}
model Message {
  id              String        @id @default(auto()) @map("_id") @db.ObjectId
  conversation    Conversation  @relation(name: "conversationMessages", fields: [conversationId], references: [id])
  conversationId  String        @db.ObjectId
  isLatestIn      Conversation? @relation("latestConversationMessage")
  sender          User          @relation(fields: [senderId], references: [id])
  senderId        String     @db.ObjectId
  body            String
  messageType     MessageType?  @default(TEXT) 
  mediaUrl        String?    
  createdAt       DateTime      @default(now())
  updatedAt       DateTime      @updatedAt
}

Prisma Engine Query

{"X":{}}
@Weakky Weakky added team/client Issue for team Client. kind/bug A reported bug. bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. tech/engines Issue for tech Engines. labels Apr 24, 2024
@Weakky
Copy link
Member

Weakky commented Apr 24, 2024

Hey @DarkWorldCoder ,

Do you have any idea, in the snippet you have shared, which query specifically reproduces the error you're seeing?

The bug you've encountered is likely specific to the data in your database. As such, it would be very helpful if you could help us create a small & isolated reproduction of your bug.

Thanks for taking the time to create an issue, cheers 🙏

@DarkWorldCoder
Copy link
Author

DarkWorldCoder commented Apr 24, 2024

@Weakky I figure out what was the issue

const conversation = await prisma.conversation.update({
    where:{
        id:conversationId
    },
    data:{
        latestMessageId:newMessage.id,
        participants:{
            update:{
                where:{
                    id:participantId
                },
                data:{
                    hasSeenLatestMessage:true,
                }
            },
            updateMany:{
                where:{
                    NOT:{
                        userId
                    }
                },
                data:{
                    hasSeenLatestMessage:false
                }
            }
        },
        isActive:conversationFind?.isActive === false ? true : true
    },
    include:conversationPopulated
})

Here, where I am performing multi update in update many I had to include conversation id to fix it otherwise it was throwing that error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. team/client Issue for team Client. tech/engines Issue for tech Engines.
Projects
None yet
Development

No branches or pull requests

2 participants