prisma2 set length and column type in prisma schema

Viewed 5955

how can i set string(varchar) length for string type be 50 and define a column be TEXT in prisma schema, for user table i want name be varchar(50) and bio be Text column. im creating my tables by prisma migrate save and up.

model User {

  id        Int      @default(autoincrement()) @id
  email     String   @unique
  password  String
  name      String   ***** varchar 50****
  bio       String  *****TEXT ??

}
2 Answers

The following feature was added recently in v2.17.0. You can specify database types in a scalar field's attributes using @db. followed by the desired native database type attribute specified by Prisma. For varchar, use the following for PostgreSQL.

model User {
  id        Int      @default(autoincrement()) @id
  email     String   @unique
  password  String
  name      String   @db.VarChar(50)
}

For more database types, refer to the Prisma Schema API docs.

@Griffin is correct. Additionally, your bio field can be left as is. Prisma sets String type as native database text types by default (only PostgreSQL & SQLite only). You can also set it explicitly regardless of the database:

model User {
  id        Int      @default(autoincrement()) @id
  email     String   @unique
  password  String
  name      String   @db.VarChar(50)
  bio       String   @db.Text
}
Related