Prisma1: Add more examples for default value directive

Created on 26 Jan 2018  Â·  5Comments  Â·  Source: prisma/prisma1

The documentation for @default can be expanded to include default values for more types:

type Post {
  title: String! @default(value: "New Post")
  published: Boolean! @default(value: "false")
  someNumber: Int! @default(value: "42")
}

Also see #1740.

aredocs

Most helpful comment

How does it work with list types?

type User {
  roles: [UserRole!]! @default(value: ["User"])
}

enum UserRole {
  User
  Admin
}

Edit: I found that the documentation states that list scalar fields don't work with @default, which is a pity. Is there a feature issue to allow this?

All 5 comments

How does it work with list types?

type User {
  roles: [UserRole!]! @default(value: ["User"])
}

enum UserRole {
  User
  Admin
}

Edit: I found that the documentation states that list scalar fields don't work with @default, which is a pity. Is there a feature issue to allow this?

@marktani Looks like it doesn't work for Int too?

attempt: Int! @default(value: "0")
 ✖ You are making a field required, but there are already nodes that would violate that constraint.

@marktani Looks like it doesn't work for Int too?

attempt: Int! @default(value: "0")
 ✖ You are making a field required, but there are already nodes that would violate that constraint.

I know this was a while back, but perhaps someone will associate the above response incorrectly.

the:

✖ You are making a field required, but there are already nodes that would violate that constraint.

Has nothing to do with the @default. Rather, the reason for that error is because there is already data stored Nodes on the database for that specific Model. By removing the ! would fix that error.

How does it work with list types?

type User {
  roles: [UserRole!]! @default(value: ["User"])
}

enum UserRole {
  User
  Admin
}

Edit: I found that the documentation states that list scalar fields don't work with @default, which is a pity. Is there a feature issue to allow this?

Can you post such documentation?

@Riseley - I found and linked the docs below for this.

Postgres
MySQL
Mongo

It also comes up as an error when attempting to run prisma deploy with @default on a scalarList that looks like the following:

✖ The @default directive must only be placed on scalar fields that are not lists.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marktani picture marktani  Â·  3Comments

notrab picture notrab  Â·  3Comments

schickling picture schickling  Â·  3Comments

sorenbs picture sorenbs  Â·  3Comments

jannone picture jannone  Â·  3Comments