question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`@default` doesnt work for enum

See original GitHub issue

I have this datamodel:

model User {
  id: Int @id
  name: String
  role: Role @default(USER)
  posts: Post[]
}

model Post {
  id: Int @id
  title: String
  author: User
}

enum Role {
  USER
  ADMIN
}

Because role has a default value, it can be omitted in the Photon API call:

import { Photon } from '@generated/photon'

const photon = new Photon()

async function main() {
  await photon.connect()

  const result = await photon.users.create({
    data: { name: 'Niko' }
  })

  console.log(result)
  photon.close()
}

main().catch(e => {
  console.error(e)
  photon.close()
})

When I execute this code, I get the following error:

$ yarn start
yarn run v1.13.0
$ ts-node main.ts
PhotonError: [
  {
    "error": "ConnectorError(NullConstraintViolation { field_name: \"User.role\" })"
  }
]
    at NodeEngine.handleErrors (/Users/nikolasburk/Desktop/prisma2-test/node_modules/@generated/photon/runtime/index.js:1308:15)
    at /Users/nikolasburk/Desktop/prisma2-test/node_modules/@generated/photon/runtime/index.js:1283:37
    at processTicksAndRejections (internal/process/task_queues.js:89:5) {
  query: 'mutation {\n  createUser(data: {\n    ' +
    'name: "Niko"\n  }) {\n    id\n    name\n  }\n' +
    '}',
  error: [
    {
      error: 'ConnectorError(NullConstraintViolation { field_name: "User.role" })'
    }
  ],
  logs: '[query-engine/core/src/builders/mutations/root.rs:179] &model.models() = ' +
    '[\n    Model {\n        name: "User",\n        stable_identifier: "",\n      ' +
    '  is_embedded: false,\n        manifestation: None,\n        fields: ' +
    'OnceCell {\n            once: Once {\n                state: Done\n         ' +
    '   },\n            value: UnsafeCell\n        },\n        ' +
    'internal_data_model: #InternalDataModelWeakRef#\n    },\n    Model {\n      ' +
    '  name: "Post",\n        stable_identifier: "",\n        is_embedded: ' +
    'false,\n        manifestation: None,\n        fields: OnceCell {\n          ' +
    '  once: Once {\n                state: Done\n            },\n            ' +
    'value: UnsafeCell\n        },\n        internal_data_model: ' +
    '#InternalDataModelWeakRef#\n    }\n]\n' +
    '[query-engine/connectors/sql-connector/src/database/sqlite.rs:54] ' +
    'visitor::Sqlite::build(q) = (\n    "INSERT INTO `migration_engine`.`User` ' +
    '(`name`) VALUES (?)",\n    [\n        Text(\n            "Niko"\n        )\n  ' +
    '  ]\n)\n',
  isPanicked: false
}
✨  Done in 1.25s.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
nelsonpecoracommented, Jul 11, 2019

I like the second one (Role.USER). It kinda weirds me out that enum values are effectively global in Prisma 1.x.

2reactions
marcjuliancommented, Jul 15, 2019

How about support of array values in default @default([value1, value2, ...])?

An example could be if a user can have multiple roles:

model User {
    id          Int       @id
    email       String    @unique
    roles       Role[]    @default([Role.ADMIN, Role.USER])
}

enum Role {
    ADMIN
    USER
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the default value for enum variable? - Stack Overflow
The default value of an enum E is the value produced by the expression (E)0 . As an example, take the following enum:...
Read more >
`@default` doesnt work for enum · Issue #33 · prisma ... - GitHub
I have this datamodel: model User { id: Int @id name: String role: Role @default(USER) posts: Post[] } model Post { id: Int...
Read more >
UnnecessaryDefaultInEnumSwitch - Error Prone
UnnecessaryDefaultInEnumSwitch. Switch handles all enum values: an explicit default case is unnecessary and defeats error checking for non-exhaustive switches.
Read more >
MySQL 8.0 Reference Manual :: 11.3.5 The ENUM Type
An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column...
Read more >
Postgres / Enum / Default enum value can't be inserted via ...
Create an ENUM in Postgres with a few options. Create a table, where one of the columns will be enum's type, and has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found