I remember reading somewhere different names can b...
# orm-help
k
I remember reading somewhere different names can be used in place of foreign keys in the schema or something. Anyone have ideas about this?
n
Hey @KJReactor 🙂 if you're referring to mapping certain DB column names (e.g. the name of a foreign key) to field names of your Prisma models, you can achieve this with `@map`. Fro example:
Copy code
model Post {
  id        Int     @default(autoincrement()) @id
  title     String
  author    User?   @relation(fields: [authorId], references: [id])
  authorId  Int?    @map("author_id")
}
If you're talking about relation fields, then you can name these whatever you like in the first place 🙂 Let me know if that helps or if you have any further questions! 🙌
b
Note that you can also map model names with
@@map
@KJReactor
💯 2
k
Many thanks @nikolasburk, I had figured out an other way though
n
No problem 🙂 curious to hear what other approach you're taking to this if you don't mind sharing 😄
k
@bntzio that is exactly what I was looking for but found an other way entirely.
My method may not be too optimal? Basically, I'm working with GraphQL along with Prisma. In my type definitions I gave the FK,
user_id
the special name,
owner
, I wanted to have in a particular type. Then, I mapped the
owner
field with the apropriate
user
field with a resolver.
It seems like
@map
may be doing something similar natively ofcourse. But with
@map
i would need to do introspection after each change to ensure it works. Am I wrong?
I just tested this and
@map
does not work on relation fields
n
I just tested this and 
@map
 does not work on relation fields
That's what I meant with "If you're talking about relation fields, then you can name these whatever you like in the first place". It doesn't make sense to use
@map
on relation fields because these don't exist in the database, so there's nothing to "map" them to 🙂
k
True @nikolasburk. However, the method you mentioned does not work in my case because I require a different name only in a particular type. All others use
user_id
just fine. It wouldn't be an issue to do the same everywhere the
user_id
FK is needed but I feel providing a better name for the relation field based on model gives more context.
👌 1