P
Prisma•3d ago
Hydroper

Dependency-dependents relations

I am having difficulty understanding whether my schema is correct, even if it reports no errors. It's basically that I'm trying to express a package registry as part of a package manager, and am having difficulty specifying dependencies and dependents relations as part of the PackageVersion model.
model PackageVersion {
id BigInt @id @default(autoincrement())
versionNumber String
publishedAt DateTime @default(now())
zip Bytes
package Package @relation(fields: [packageId], references: [id])
packageId BigInt
dependencies PackageVersion[] @relation("Dependency")
dependents PackageVersion[] @relation("Dependency")
yanked Boolean @default(false)
}
model PackageVersion {
id BigInt @id @default(autoincrement())
versionNumber String
publishedAt DateTime @default(now())
zip Bytes
package Package @relation(fields: [packageId], references: [id])
packageId BigInt
dependencies PackageVersion[] @relation("Dependency")
dependents PackageVersion[] @relation("Dependency")
yanked Boolean @default(false)
}
2 Replies
Hydroper
HydroperOP•2d ago
I give up on Prisma -_-
RaphaelEtim
RaphaelEtim•this hour
Hi 👋 I think the confusion stems from having the same relation name Dependency for both dependencies and dependents fields. Also, from your code, you are trying to create a self-referencing many-to-many relationship for package dependencies. To properly express this relationship, you need to use distinct names for each side of the relation and add the necessary fields to make it an explicit many-to-many relationship. For example:
model PackageVersion {
id BigInt @id @default(autoincrement())
versionNumber String
publishedAt DateTime @default(now())
zip Bytes
package Package @relation(fields: [packageId], references: [id])
packageId BigInt
dependencies PackageDependency[] @relation("DependencyTo")
dependents PackageDependency[] @relation("DependencyFrom")
yanked Boolean @default(false)
}

model PackageDependency {
id BigInt @id @default(autoincrement())
dependencyVersion PackageVersion @relation("DependencyTo", fields: [dependencyVersionId], references: [id])
dependencyVersionId BigInt
dependentVersion PackageVersion @relation("DependencyFrom", fields: [dependentVersionId], references: [id])
dependentVersionId BigInt

@@unique([dependencyVersionId, dependentVersionId])
}
model PackageVersion {
id BigInt @id @default(autoincrement())
versionNumber String
publishedAt DateTime @default(now())
zip Bytes
package Package @relation(fields: [packageId], references: [id])
packageId BigInt
dependencies PackageDependency[] @relation("DependencyTo")
dependents PackageDependency[] @relation("DependencyFrom")
yanked Boolean @default(false)
}

model PackageDependency {
id BigInt @id @default(autoincrement())
dependencyVersion PackageVersion @relation("DependencyTo", fields: [dependencyVersionId], references: [id])
dependencyVersionId BigInt
dependentVersion PackageVersion @relation("DependencyFrom", fields: [dependentVersionId], references: [id])
dependentVersionId BigInt

@@unique([dependencyVersionId, dependentVersionId])
}
Looking at the example i shared, I've introduced a new PackageDependency model to represent the many-to-many relationship explicitly. The dependencies field in PackageVersion now uses the relation name DependencyTo, representing the versions that this package depends on. The dependents field in PackageVersion uses the relation name DependencyFrom, representing the versions that depend on this package. The PackageDependency model has two fields referencing PackageVersion, one for the dependency and one for the dependent. You may also want to share your full schema in order for us to help you further.

Did you find this page helpful?