Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@default doesnt work for enum #33

Closed
nikolasburk opened this issue Jun 10, 2019 · 9 comments
Closed

@default doesnt work for enum #33

nikolasburk opened this issue Jun 10, 2019 · 9 comments
Assignees
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug.
Milestone

Comments

@nikolasburk
Copy link
Member

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.
@matthewmueller matthewmueller transferred this issue from another repository Jun 27, 2019
@schickling schickling assigned schickling and unassigned dpetrick Jun 28, 2019
@schickling
Copy link
Member

We should think through the syntax for this again. Instead of @default(USER), I'd rather be in favor in one of the following options:

  1. @default(.USER)
  2. @default(Role.USER)

@marcjulian
Copy link
Contributor

marcjulian commented Jul 11, 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
}

@nelsonpecora
Copy link

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

@pantharshit00 pantharshit00 added the kind/feature A request for a new feature. label Aug 23, 2019
@janpio janpio assigned matthewmueller and unassigned schickling Dec 13, 2019
@janpio janpio added bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. and removed kind/feature A request for a new feature. labels Dec 13, 2019
@matthewmueller matthewmueller removed their assignment Jan 28, 2020
@matthewmueller

This comment has been minimized.

@divyenduz divyenduz added this to the Preview 25 milestone Mar 13, 2020
@dpetrick
Copy link
Contributor

Working on the engine level with direct GraphQL query.

Datamodel needs to change (the one posted is quite old):

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

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

enum Role {
  USER
  ADMIN
}

Query:

mutation {
  createOneUser(data:{
    id: 2                 // now needed because of the old implicit autoincrement behaviour on int IDs
    name: "Test"
  }) {
    id
    role
  }
}

Result:

{
  "data": {
    "createOneUser": {
      "id": 2,
      "role": "USER"
    }
  }
}

@janpio
Copy link
Member

janpio commented Mar 16, 2020

I adapted the reproduction above to alpha 917:

  1. prisma2 init
  2. Edit datasource.provider to sqlite, datasource.url to "file:dev.db"
  3. Append schema:
    model User {
      id Int @id @default(autoincrement())
      name String
      role Role @default(value: USER)
      posts Post[]
    }
    
    model Post {
      id Int @id
      title String
      author User
    }
    
    enum Role {
      USER
      ADMIN
    }
    (This also has `autoincrement() to not make explicit ID necessary)
  4. prisma2 migrate save --experimental and prisma2 migrate up --experimental
  5. Create index.js with content:
    const { PrismaClient } = require('@prisma/client')
    
    const prisma = new PrismaClient()
    
    async function main() {
      await prisma.connect()
    
      const result = await prisma.User.create({
        data: { name: 'Niko' }
      })
    
      console.log(result)
    }
    
    main().catch(e => {
      console.error(e)
    })

This leads to this output:

C:\Users\Jan\Documents\throwaway\33>node index.js
{ id: 2, name: 'Niko', role: 'USER' }
^C
C:\Users\Jan\Documents\throwaway\33>
C:\Users\Jan\Documents\throwaway\33>node index.js
{ id: 3, name: 'Niko', role: 'USER' }
^C
C:\Users\Jan\Documents\throwaway\33>node index.js
{ id: 4, name: 'Niko', role: 'USER' }
^C
C:\Users\Jan\Documents\throwaway\33>

Is this the expected behavior @nikolasburk and this bug can be closed?

@janpio janpio assigned janpio and unassigned do4gr Mar 16, 2020
@nikolasburk
Copy link
Member Author

Looks great, thanks again :)

@thilllon
Copy link

thilllon commented May 17, 2022

@johnsonfash
Copy link

enum permission {
read
create
update
delete
}

model User {
permission permission[] @default([read, update])
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug.
Projects
None yet
Development

No branches or pull requests