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

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/29

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

痛点

用户在使用 Prisma 定义数据模型时,希望为 ID 字段设置一个以固定前缀(如“user”)开头、后接动态生成字符串的默认值,例如通过 JavaScript 表达式或函数生成。但 Prisma 的 @default 指令目前不支持执行 JavaScript 代码或字符串插值,用户只能使用静态字符串或内置函数。这导致用户无法在模型层直接实现自定义 ID 生成逻辑,被迫在应用层手动处理,增加了代码复杂性和维护成本,且可能破坏数据一致性。

§ 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
af9a675f-0f7d-4151-8bd1-a81f05ff13d2
rank_date
2026-05-30
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": 295
  },
  "question_id": 61585219,
  "answer_fetch": {
    "has_more": false,
    "answers_fetched": 0,
    "answer_page_size": 3
  },
  "snapshot_version": "stackoverflow_question_v1"
}
source_raw_snapshot
{
  "id": "f8652591-9abc-480f-b8f8-09e2dfd2f6f1",
  "daily_ranking_item_id": "af9a675f-0f7d-4151-8bd1-a81f05ff13d2",
  "source": "stackoverflow",
  "external_id": "61585219",
  "fetched_at": "2026-05-29T22:02:13.965Z",
  "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": 295
    },
    "answer_fetch": {
      "backoff": null,
      "has_more": false,
      "answers_fetched": 0,
      "quota_remaining": 267,
      "answer_page_size": 3
    },
    "snapshot_version": "stackoverflow_question_v1",
    "selection_strategy": "tag_whitelist_unanswered_high_score_recent_active"
  },
  "created_at": "2026-05-29T22:02:14.201Z",
  "updated_at": "2026-05-29T22:02:14.201Z"
}