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

Relations over composite types #10102

Open
Tracked by #8788 ...
pimeys opened this issue Nov 4, 2021 · 3 comments
Open
Tracked by #8788 ...

Relations over composite types #10102

pimeys opened this issue Nov 4, 2021 · 3 comments
Labels
kind/feature A request for a new feature. team/client Issue for team Client. team/psl-wg team/schema Issue for team Schema. tech/engines/introspection engine Issue in the Introspection Engine tech/engines/migration engine Issue in the Migration Engine tech/engines/query engine Issue in the Query Engine topic: composite-types topic: mongodb topic: relationMode formerly `referentialIntegrity`

Comments

@pimeys
Copy link
Contributor

pimeys commented Nov 4, 2021

If we have referential integrity handled in Prisma, we could have relations from composite types.

@pimeys pimeys added tech/engines/migration engine Issue in the Migration Engine tech/engines/query engine Issue in the Query Engine tech/engines/introspection engine Issue in the Introspection Engine team/schema Issue for team Schema. topic: mongodb labels Nov 4, 2021
@janpio janpio added kind/feature A request for a new feature. team/psl-wg labels Nov 10, 2021
@janpio janpio added the team/client Issue for team Client. label Dec 16, 2021
@janpio janpio added the topic: relationMode formerly `referentialIntegrity` label Jan 28, 2022
@janpio janpio mentioned this issue Jul 1, 2022
35 tasks
@maxime4000
Copy link

maxime4000 commented Feb 6, 2023

Let's said we have this model:

datasource db {
  provider = "mongodb"
  url      = env("DB_URL")
}

generator client {
  provider = "prisma-client-js"
}

model Country {
  id     String         @id @default(auto()) @map("_id") @db.ObjectId
  code   String
  name   String
  states CountryState[]
}

type CountryState {
  id        String @default(auto()) @db.ObjectId
  name      String
  stateCode String
  // Error validating: Defining `@relation` attribute for a field in a composite type is not allowed.
  taxe      Taxe?   @relation(fields: [taxeId], references: [id])
  taxeId    String? @db.ObjectId
}

model Taxe {
  id        String       @id @default(auto()) @map("_id") @db.ObjectId
  countryId String       @db.ObjectId
  country   Country      @relation(fields: [countryId], references: [id])
  state     CountryState @relation(fields: [stateId], references: [id])
  stateId   String       @db.ObjectId
  // ...
}

Could relations over compositeTypes work something like that ?

Our actual use case is to embedded a lightweight copy of the State and Country in Taxe (ex: {id, name, value}). So we have a Composite Type for TaxeCountry and TaxeState. Tbh, I'm not sure if what I wrote is optimized, embedding data is faster than referencing, but I would like to be able to fetch taxe from Countries' States. Because Country own multiple composite Type of CountryState, I cannot lookup Taxe from CountryState. Could do in a raw query format, but hey, if I don't have to, would be better.

If I change CountryState to model, I can remove countryId in Taxe and just reference the State. Being able to get the country coming from Taxe, which is what I could want here.

Note: I know the model wouldn't benefit much of nesting value for optimization, but I'm asking because we have better use case that would benefit having @relation in nested data.

@janpio
Copy link
Member

janpio commented Jul 17, 2023

While doing some research for #20217 I stumbled over the fact that MongoDB does support indexes on composite types / embedded documents, but not unique indexes:

https://www.mongodb.com/community/forums/t/unique-indexes-on-embedded-documents/16825
https://www.mongodb.com/community/forums/t/building-unique-index-on-array-of-embedded-documents/105981

That would be something to figure out when we implement this, as 1:1 relations are enforced via @unique.

@edobrb
Copy link

edobrb commented Mar 24, 2024

Hi, does anyone know if this issue will be included in the roadmap or if it will be left behind?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/client Issue for team Client. team/psl-wg team/schema Issue for team Schema. tech/engines/introspection engine Issue in the Introspection Engine tech/engines/migration engine Issue in the Migration Engine tech/engines/query engine Issue in the Query Engine topic: composite-types topic: mongodb topic: relationMode formerly `referentialIntegrity`
Projects
None yet
Development

No branches or pull requests

4 participants