返回 Discover
Field DispatchStack Overflow10 · 2026-05-31

Prisma: Is there any way to execute javascript as default value of prisma data type?

Tags
javascriptgraphqlgraphql-jsprismaprisma-graphql
Score
5
Answers
0
Views
1,142
Answered
No
痛点分析发布于 2026/05/30

痛点为 AI 基于上游原始证据的初步提炼;未包含额外中国市场检索。

痛点

用户在使用 Prisma 定义数据模型时,希望为 ID 字段设置一个以特定前缀开头的动态默认值(如 `user` 后接 JavaScript 表达式结果),但 Prisma 的 `@default` 指令不支持 JavaScript 函数或字符串插值。这导致用户无法在模型层直接实现自定义 ID 生成逻辑,被迫在应用代码中手动处理 ID 赋值,增加了代码复杂性和出错风险。由于该问题没有获得任何回答,且提问者未接受答案,说明 Prisma 官方文档和社区尚未提供清晰的解决方案,用户可能需要在多个方案间反复尝试,造成时间浪费和开发效率下降。

§ Dossier

Stack Overflow question

in my data model, as the type of id, I want to generate a default value that will start with the user. so the id will bed something like this user..rear33422qeqr type User { myId: String! @default (`user${2+2}`) name: String! email: String! @unique } is it possible to call a javascript function or using string interpolation ( user${2+2} )

§ Dossier

Question details

View count
1,142
Answer count
0
Last activity
2026/05/28
源数据· Raw Archive
source
Stack Overflow
upstream_source
stackoverflow
upstream_item_id
61585219
daily_ranking_item_id
e1f1d5f4-4a4d-4723-9be6-4ad4a146556b
rank_date
2026-05-31
rank
10
name
Prisma: Is there any way to execute javascript as default value of prisma data type?
tagline
javascript, graphql, graphql-js, prisma, prisma-graphql
description
in my data model, as the type of id, I want to generate a default value that will start with the user. so the id will bed something like this user..rear33422qeqr type User { myId: String! @default (`user${2+2}`) name: String! email: String! @unique } is it possible to call a javascript function or using string interpolation ( user${2+2} )
votes_count
5
comments_count
0
created_at_on_source
2020-05-04T04:31:19.000Z
topics
javascriptgraphqlgraphql-jsprismaprisma-graphql
media / source-specific data
{
  "stackoverflow": {
    "score": 5,
    "view_count": 1142,
    "is_answered": false,
    "top_answers": [],
    "answer_count": 0,
    "accepted_answer_id": null,
    "last_activity_date": 1779993985
  }
}
raw_payload
{
  "stats": {
    "score": 5,
    "view_count": 1142,
    "is_answered": false,
    "answer_count": 0,
    "creation_date": 1588566679,
    "last_edit_date": null,
    "accepted_answer_id": null,
    "last_activity_date": 1779993985
  },
  "api_wrapper": {
    "backoff": null,
    "has_more": true,
    "page_size": 8,
    "quota_max": 300,
    "quota_remaining": 206
  },
  "question_id": 61585219,
  "answer_fetch": {
    "has_more": false,
    "answers_fetched": 0,
    "answer_page_size": 3
  },
  "snapshot_version": "stackoverflow_question_v1"
}
source_raw_snapshot
{
  "id": "a0cce26b-0465-497c-bc16-3ec8cb0f3932",
  "daily_ranking_item_id": "e1f1d5f4-4a4d-4723-9be6-4ad4a146556b",
  "source": "stackoverflow",
  "external_id": "61585219",
  "fetched_at": "2026-05-30T22:02:04.421Z",
  "question_raw": {
    "body": "<p>in my data model, as the type of id, I want to generate a default value that will start with the user.</p>\n\n<p>so the id will bed something like this <code>user..rear33422qeqr</code></p>\n\n<pre><code>type User {\n\n  myId: String! @default (`user${2+2}`)\n  name: String!\n  email: String! @unique\n\n}\n</code></pre>\n\n<p>is it possible to call a javascript function or using string interpolation (<code>user${2+2}</code>)</p>\n",
    "link": "https://stackoverflow.com/questions/61585219/prisma-is-there-any-way-to-execute-javascript-as-default-value-of-prisma-data-t",
    "tags": [
      "javascript",
      "graphql",
      "graphql-js",
      "prisma",
      "prisma-graphql"
    ],
    "owner": {
      "link": "https://stackoverflow.com/users/5534469/ashik",
      "user_id": 5534469,
      "user_type": "registered",
      "account_id": 7255753,
      "reputation": 3508,
      "display_name": "Ashik",
      "profile_image": "https://www.gravatar.com/avatar/c100d4bf62af61c3cce61692945b8c4d?s=256&d=identicon&r=PG&f=y&so-version=2"
    },
    "score": 5,
    "title": "Prisma: Is there any way to execute javascript as default value of prisma data type?",
    "view_count": 1142,
    "is_answered": false,
    "question_id": 61585219,
    "answer_count": 0,
    "creation_date": 1588566679,
    "content_license": "CC BY-SA 4.0",
    "last_activity_date": 1779993985
  },
  "answers_raw": [],
  "tags_raw": [
    "javascript",
    "graphql",
    "graphql-js",
    "prisma",
    "prisma-graphql"
  ],
  "stats_raw": {
    "score": 5,
    "view_count": 1142,
    "is_answered": false,
    "answer_count": 0,
    "creation_date": 1588566679,
    "last_edit_date": null,
    "accepted_answer_id": null,
    "last_activity_date": 1779993985
  },
  "selection_meta": {
    "site": "stackoverflow",
    "api_wrapper": {
      "backoff": null,
      "has_more": true,
      "page_size": 8,
      "quota_max": 300,
      "quota_remaining": 206
    },
    "answer_fetch": {
      "backoff": null,
      "has_more": false,
      "answers_fetched": 0,
      "quota_remaining": 177,
      "answer_page_size": 3
    },
    "snapshot_version": "stackoverflow_question_v1",
    "selection_strategy": "tag_whitelist_unanswered_high_score_recent_active"
  },
  "created_at": "2026-05-30T22:02:04.701Z",
  "updated_at": "2026-05-30T22:02:04.701Z"
}