Hey guys could use some help, been at this all nig...
# orm-help
p
Hey guys could use some help, been at this all night with no luck, geatly appreciate it
Copy code
model Domain {
  id                 String              @id @default(cuid())
  domain             String              @unique
  majesticDomainData MajesticDomainData?
}

model MajesticDomainData {
  id                     String                @id @default(cuid())
  domain                 Domain                @relation(fields: [domainId], references: [id])
  domainId               String                @unique
  topics                 MajesticTopicalData[]
}

model MajesticTopicalTrustFlow {
  id         String                @id @default(cuid())
  topic      String                @unique
  domainData MajesticTopicalData[]
}

model MajesticTopicalData {
  domainData         MajesticDomainData       @relation(fields: [domainDataId], references: [id])
  domainDataId       String
  topicalTrustFlow   MajesticTopicalTrustFlow @relation(fields: [topicalTrustFlowId], references: [id])
  topicalTrustFlowId String
  topicalPercent     Decimal                  @db.Decimal(5, 4)

  @@id([domainDataId, topicalTrustFlowId])
}
Copy code
prisma.domain.update({
                            where: {<http://asd.com|asd.com>},
                            data: {
                                majesticDomainData: {
                                    create: {
                                        topics: {
                                            create: [
                                                {
                                           topic: 'health',
                                           topicalPercent: 0.24
                                                },
                                                {
                                          topic: 'rec',
                                         topicalPercent: 0.33
                                                },
                                            ]
                                        },
                                    }
                                }
                            },
                        })
Copy code
+             topicalTrustFlow: {
+               create?: MajesticTopicalTrustFlowCreateWithoutDomainInput | MajesticTopicalTrustFlowUncheckedCreateWithoutDomainInput,
+               connectOrCreate?: MajesticTopicalTrustFlowCreateOrConnectWithoutDomainInput,
+               connect?: MajesticTopicalTrustFlowWhereUniqueInput
+             }
Im not sure if im connecting the many to many relationship correctly, its asking for my explicit table
👀 2
g
what's the problem?
r
Hi @Peter 👋 Can you please describe what the issue is so we can know how to help?
p
I have a domain table with a one to one with majesticdomaindata table and that has a many to many relationship with MajesticTopicalTrustFlow table and I am connecting them using the explicit MajesticTopicalData table but I am having trouble connecting the tables properly
r
Let me reproduce this scenario, I'll get back to you on this.
p
Are there any glaring mistakes within the prisma.domain.update code?
Thank you @Raphael Etim been at this way to long, its nice to get a fresh set of eyes on this
r
From what i can see,
topic
is not a property of
MajesticTopicalData
. You should not have topic as part of the fields.
You can have either of
topicalTrustFlow
and
topicalPercent
p
So im assuming there is no way I can create the topic through here. Ive also tried adding the tropicaltrustflow
Copy code
topics: {
                                            create: [
                                                {
                                                    topicalPercent: 0.24,
                                                    topicalTrustFlow: {
                                                        create: {topic: 'asdfsd'}
                                                    }
                                                },
                                                // {
                                                //     topicalPercent: 0.44
                                                // },
                                            ]
                                        },
But get this error:
Copy code
The change you are trying to make would violate the required relation 'DomainToMajesticDomainData' between the `Domain` and `MajesticDomainData` models.

  code: 'P2014',
  clientVersion: '4.1.1',
  meta: {
    relation_name: 'DomainToMajesticDomainData',
    model_a_name: 'Domain',
    model_b_name: 'MajesticDomainData'
  }
}
r
I'm looking into it
p
Updated to do this and it now I am getting a unique contraint error:
Copy code
topics: {
                                create: [
                                    {
                                        topicalPercent: 0.514,
                                        topicalTrustFlow: {
                                            connectOrCreate: {
                                                where: {topic: 'Health'},
                                                create: {topic: 'Health'}
                                            }
                                        }
                                    },
                                    {
                                        topicalPercent: 0.15,
                                        topicalTrustFlow: {
                                            connectOrCreate: {
                                                where: {topic: 'Rec'},
                                                create: {topic: 'Rec'}
                                            }
                                        }
                                    }
                                ]
                            },
Copy code
Response error PrismaClientKnownRequestError: 
Invalid `prisma.domain.update()` invocation:


  Unique constraint failed on the (not available)
Thought it might be a race condition with connectOrCreate so i inserted all the majstic topics before and tried using connect but am still getting unique key constraint
Copy code
topics: {
                                create: [
                                    {
                                        topicalPercent: 0.5314,
                                        topicalTopic: {
                                                connect: {topic: 'Health/Medicine'}
                                        }
                                    },
                                    {
                                        topicalPercent: 0.315,
                                        topicalTopic: {
                                                connect: {topic: 'Science'}
                                        }
                                    }
                                ]
                            },
I noticed on a completely new table the first try only inserts the topics with createMany but no other data. The 2nd try properly inserts all the data. The 3rd try gives me the unique constraint error
Copy code
majesticDomainData: {
                                    upsert: {
                                        create: {},
                                        update: majesticDomainData,
                                    }
                                }
With this:
Copy code
majesticDomainData: {
                                    create: majesticDomainData,
                                }
I am getting this error:
Copy code
The change you are trying to make would violate the required relation 'DomainToMajesticDomainData' between the `Domain` and `MajesticDomainData` models.

  code: 'P2014',
  clientVersion: '4.1.1',
  meta: {
    relation_name: 'DomainToMajesticDomainData',
    model_a_name: 'Domain',
    model_b_name: 'MajesticDomainData'
  }
}
here is my majesticDomainData:
Copy code
const majesticDomainData = {
                            topics: {
                                create: [
                                    {
                                        topicalPercent: 0.5314,
                                        topicalTopic: {
                                            connect: {topic: 'Health/Medicine'}
                                        }
                                    },
                                    {
                                        topicalPercent: 0.315,
                                        topicalTopic: {
                                            connect: {topic: 'Science'}
                                        }
                                    }
                                ]
                            }
                        }
r
Hi @Peter, I've shared this query with another team member and we'll try to come up with a potential solution.
n
Hey Peter, According to me this error is occurring because you are creating a
MajesticDomainData
record without creating a
domain
record. This would result in violating one-to-one relation between them.
The change you are trying to make would violate the required relation ‘DomainToMajesticDomainData’ between the
Domain
and
MajesticDomainData
models
I am trying to understand what’s the exact use case that you are trying to achieve.
p
I use a create many to add domains
Copy code
const addDomains = await prisma.domain.createMany({
                    data: allDomains,
                    skipDuplicates: true
                });
What Im trying to do is add the majesticDomainData to the domain itself
Copy code
return prisma.domain.update({
                            where: {domain: p.domain},
                            data: {
                                majesticDomainData: {
                                    create: majesticDomainData,
                                }
                            },
                            select: {
                                domain: true
                            }
                        })
n
Is your code perhaps open-source? If yes, can you share it with us even if the query doesn’t work, I can have a look at it and try to fix it by trying to have a holistic view of your app.