hey all! Is there any way to turn off foreign key ...
# orm-help
s
hey all! Is there any way to turn off foreign key constraint generation when I run
prisma db push
? Here is my schema:
Copy code
model profiles {
  id                   String          @id
  first_name           String?
  last_name            String?
  phone_number         String          @unique
  phone_number_ref     contacts[]
  contacts_ref         contacts[]      @relation("contact_holder")
}

model contacts {
  id                 Int       @id
  contact            String?
  contact_ref        profiles? @relation(fields: [contact], references: [phone_number])
  contact_holder     String
  contact_holder_ref profiles  @relation("contact_holder", fields: [contact_holder], references: [id])
}
Every profile has an
id
and a
phone_number
. Each user in the
profiles
table will have each of their contacts on their phone put into
contacts
as individual rows.
contact_holder_ref
will always reference a user
id
in the
profiles
table. But a user’s contacts might may or may not reference another profile’s
phone_number
in the
profiles
table. Foreign key constraints are getting created by prisma that prevent me from adding phone numbers in
contacts
that don’t exist in
profiles
. Is there anything I can do to stop
contacts_contact_fkey
from getting created? Everything seems to work when I manually delete the constraint, but then I can’t run
prisma db push
again.
c
it’s a little convoluted, but we solved a similar problem by adding an explicit intermediate table like PhoneNumbers that has optional linkage to both tables. that’s assuming i understand the problem correctly
s
How do you create an optional linkage? It seems like if there was a way to optionally link two tables then that would solve my problem. Right now at least, a profile with phone_number x must exist, if i’m going to create a new row in contacts where contact is also x. Making the fields contact and contact_ref optional doesn’t seem to make the relationship optional.
c
iirc, we just added a ? argument to the end of the relation fields (e.g
phone_number: PhoneNumber?
and
phone_number_id: String?
)
s
When I try to create optional relations, a key constraint keeps getting created. Is there no way to specify that I don’t want to have a key constraint?
r
Prisma doesn’t work without foreign key constraints. We have a request for this here that you can add a 👍 to and even your use case in the comments.
🙌 1