Prisma如何在数据库表中包含连字符时创建模型?

huangapple go评论81阅读模式
英文:

Prisma how to create a model when database table has hyphen in it?

问题

我在我的数据库中有带连字符的表,例如"user-cars"。我无法将名称更改为"user_cars"。有没有办法将模型命名为"user_cars",但使其引用"user-cars"表?我尝试了@@map("user_cars"),但没有成功。

英文:

i have tables in my database with hyphen, like "user-cars". I cannot change the name to "user_cars". Is there a way to name the model as "user_cars" but make it reference the "user-cars" table? I tried @@map("user_cars"), but it didn't work.

答案1

得分: 1

根据文档,你应该将模型映射到底层表名。你尝试过 user_cars,但你试过 user-cars 吗?如果你的表名是 user-cars,那么你应该像这样将其映射到你的模型:

model UserCar {
  // 字段

  @@map("user-cars")
}

他们的示例是:

然而,你仍然可以选择将模型命名为 Comment(例如,遵循命名约定),而不需要在数据库中重命名底层的 comments 表,只需使用 @@map 属性:

model Comment {
  // 字段

  @@map("comments")
}

通常模型名称遵循的约定是,底层表名是复数形式,而模型名称本身是单数形式。理想情况下,你应该使用 UserCar 作为模型名称,而不是 user_cars

希望有所帮助!

英文:

According to the docs, you'd map the model to the underlying table name. You tried user_cars but did you try user-cars? If your table name is user-cars then you should map that to your model like:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

model UserCar {
  // Fields

  @@map(&quot;user-cars&quot;)
}

<!-- end snippet -->

https://www.prisma.io/docs/concepts/components/prisma-schema/names-in-underlying-database

Their example is:

> However, you can still choose Comment as the name of the model (e.g.
> to follow the naming convention) without renaming the underlying
> comments table in the database by using the @@map attribute:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

model Comment {
  // Fields

  @@map(&quot;comments&quot;)
}

<!-- end snippet -->

The model names typically follow the convention where the underlying table is plural, and the model name itself is singular. Ideally you should use UserCar as the model name instead of user_cars.

https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#naming-conventions

I hope that helps!

huangapple
  • 本文由 发表于 2023年2月9日 01:34:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定