Hi guys, is it possible to make 1-1 relationship w...
# orm-help
h
Hi guys, is it possible to make 1-1 relationship with prisma?
l
Yes, same as 1-N relation, you just specify it's a single entity and not an array, on both sides
h
I get this error when i provide @relation on both sides:
Copy code
Error parsing attribute "@relation": The relation fields `warehouse` on Model `AccountAddress` and `address` on Model `Warehouse` both provide the `references` argument in the @relation attribute. You have to provide it only on one of the two fields.
l
not
@relation
on both sides - just need to put it on one of the sides
Copy code
model A {
  id Int @id
  bId Int
  bEntity B @relation(fields: [bId], references: [id])
}

model B {
  id Int @id
  aEntity A 
}
something like this should work - entity A is the one holding the foreign key, but the connection should be available on both sides
h
Then it will automatically create a many relationship on the otherside
Aah i see. I was able to do like:
Copy code
model B {
 aEntity A?
}
l
yea it can be optional if you need to
h
Thanks! Learned something new then 😄
🙌 1
r
@Harun 👋 The virtual field always needs to be optional in a 1-1 relation 🙂
l
I think on one side it has to by optional, and I would think it'S on the side without the FK kind of makes sense (going from my example) - if you want to create entity A and "connect" it to entity B, that entity B already has to exist and it doesn't have its entity A yet and even if you create them in a nested write, B will probably be created first and again, it doesn't have its entity A created yet
h
@Lukáš Stuchlík That makes sense!
@Ryan Thanks Ryan 🙂