Hi :slightly_smiling_face:, very new Prisma user h...
# orm-help
a
Hi 🙂, very new Prisma user here, I really like the Project so far (only ORM that could generate proper types)! I have a question on how to create a many to many relationship with having a
mult-field id
: I have two tables,
generic_articles
and
translations
. Multiple articles can have the same name, so
generic_article_name
is not
@unique
, and one
generic_article_name
returns all translations for that name, making it a many to many relationship.
Copy code
model generic_articles {
  generic_article_id                                      Int                                                       @id
  generic_article_name                                    Int                                                       
  generic_article_names                                   generic_article_names[]
}


model translations {
  term_id          Int
  language_id      Int
  term             String             @db.VarChar(60)
  generic_articles generic_article_names[]

  @@id([term_id, language_id])
}


model generic_article_names {
  generic_article    generic_articles @relation(fields: [generic_article_id], references: [generic_article_name])
  generic_article_id Int
  translation        translations     @relation(fields: [term_id], references: [term_id])
  term_id            Int

  @@id([generic_article_id, term_id])
}
I tried following this, but it didn't work out as it needs to reference
@id
, but the
generic_article_name
is not unique. Currently I "pretend" for it to be unique and then I find duplicates of the
generic_article_name
and put the data manually onto the duplicates.
r
@Albert Luft 👋 Yes the
id
will be required so you would either need to fetch the
id's
of all the names you want to create above or just the specific
id
to create a many-to-many relationship. To
connect
to the specific record, a unique field is required so there’s no other way around that 🙂
a
@Ryan Thank you for the answer, if you are enticed by internet points you can copy paste it to SO here, and I shall accept it 😄 My trick so far was to set
generic_article_names
as unique and then fill out the ones that were duplicated manually hahah, so I guess I'll continue doing that
👍 1
d
Yeah @Albert Luft What Ryan said is correct, you need a unique key for this relation, if you need any help implementing it, let me know
a
@Daniel Olavio Ferreira If you could spare the time would be awesome ! I am very new to sql in general 🙂
d
@Albert Luft I believe this will work, could you please check
model generic_articles {
  generic_article_id      Int                    @id @default(autoincrement())   generic_article_name    Int   generic_article_names   
_generic_article_names_? @relation(fields: [generic_article_namesId], references: [id])
  generic_article_namesId Int? } model translations {   id                      Int                    @id @default(autoincrement())   term_id                 Int   language_id             Int   term                    String                 @db.VarChar(60)   generic_article_names   
_generic_article_names_? @relation(fields: [generic_article_namesId], references: [id])
  generic_article_namesId Int? } model generic_article_names {   id                 Int                @id @default(autoincrement())   generic_articles   
_generic_articles_[]
  generic_article_id Int   term_id            Int   translations       
_translations_[]
}