ERROR", parsed_severity: Some(Error), code: SqlState(E22003), message: "integer out of range for type int4

This  error occurs whenever I try to add a new object to my model on Next js connected to a postgresql database hosted on CockroachDB using prisma client.

Type: undefined
Message: 
Invalid `prisma.author.create()` invocation:


Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Error { kind: Db, cause: Some(DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState(E22003), message: "integer out of range for type int4", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("eval.go"), line: Some(35), routine: Some("init") }) }), transient: false })

Code: undefined

Query:
[object Object]

Here are the contents of my schema file

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

// datasource db {
//   provider = "sqlite"
//   // url      = env("DATABASE_URL")
//   url      = "file:./dev.db"
// }

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Account {
  id                String  @id @default(cuid())
  userId            String
  type              String
  provider          String
  providerAccountId String
  refresh_token     String? //@db.Text
  access_token      String? //@db.Text
  expires_at        Int?
  token_type        String?
  scope             String?
  id_token          String? //@db.Text
  session_state     String?

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
}

model Session {
  id           String   @id @default(cuid())
  sessionToken String   @unique
  userId       String
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  author        Author[]
  comment       Comment[]
  createdAt     DateTime  @default(now())
}

model VerificationToken {
  identifier String
  token      String   @unique
  expires    DateTime

  @@unique([identifier, token])
}

model Author {
  id      Int             @id @default(autoincrement())
  name    String
  email   String    @default("unknown")
  image   String
  bio     String
  user    User      @relation(fields: [userId], references: [id])
  userId  String
  article Article[]
}

model Category {
  id      String    @id @default(cuid())
  name    String    @unique
  article Article[]
}

model Article {
  id         Int             @id @default(autoincrement())
  author     Author    @relation(fields: [authorId], references: [id])
  authorId   Int
  title      String
  body       String
  image      String
  summary    String
  category   Category  @relation(fields: [categoryId], references: [id])
  categoryId Int
  slug       String    @unique @default("Uknown")
  views      Int       @default(0)
  createdAt  DateTime  @default(now())
  comment    Comment[]
  tags       String    @default("unknown")
}

model Comment {
  id        Int             @id @default(autoincrement())
  user      User     @relation(fields: [userId], references: [id])
  userId    String
  body      String
  createdAt DateTime @default(now())
  article   Article  @relation(fields: [articleId], references: [id])
  articleId Int
}

 

message profile
Admin
2023-05-24

This error occurs becuse you are using Int as your default primarykey(i.e your Id). I faced this error when i used cockroachdb's postgresql database.

The easy fix is to convert all your Int primarykeys to strings and use a collision resistant Id i.e a CUID as shown below

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

// datasource db {
//   provider = "sqlite"
//   // url      = env("DATABASE_URL")
//   url      = "file:./dev.db"
// }

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Account {
  id                String  @id @default(cuid())
  userId            String
  type              String
  provider          String
  providerAccountId String
  refresh_token     String? //@db.Text
  access_token      String? //@db.Text
  expires_at        Int?
  token_type        String?
  scope             String?
  id_token          String? //@db.Text
  session_state     String?

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
}

model Session {
  id           String   @id @default(cuid())
  sessionToken String   @unique
  userId       String
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
  id            String    @id @default(cuid()) 
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  author        Author[]
  comment       Comment[]
  createdAt     DateTime  @default(now())
}

model VerificationToken {
  identifier String
  token      String   @unique
  expires    DateTime

  @@unique([identifier, token])
}

model Author {
  id      String   @id @default(cuid())
  name    String
  email   String    @default("unknown")
  image   String
  bio     String
  user    User      @relation(fields: [userId], references: [id])
  userId  String
  article Article[]
}

model Category {
  id      String    @id @default(cuid()) //use cuid here
  name    String    @unique
  article Article[]
}

model Article {
  id         String   @id @default(cuid()) //use cuid here
  author     Author    @relation(fields: [authorId], references: [id])
  authorId   String
  title      String
  body       String
  image      String
  summary    String
  category   Category  @relation(fields: [categoryId], references: [id])
  categoryId String
  slug       String    @unique @default("Uknown")
  views      Int       @default(0)
  createdAt  DateTime  @default(now())
  comment    Comment[]
  tags       String    @default("unknown")
}

model Comment {
  id        String   @id @default(cuid()) //use cuid here
  user      User     @relation(fields: [userId], references: [id])
  userId    String
  body      String
  createdAt DateTime @default(now())
  article   Article  @relation(fields: [articleId], references: [id])
  articleId String
}

 

Add Message

Click on the button below to add a new message to this thread

Tags

#Javascript #deployment #next js

Thread detail

Satus: Open
Messages: 1Started: 2023-05-24
loading..

DEVMAESTERS

Newsletter

Services

Frontend Development |Backend Development |Full Website Development |Bootstrap Website upgrades | Website Debbugging | Website Hosting & deployment

Contact

Interested in hiring me or collaborating with me on a project, click on any of the links below to get my social media handle

Or contact me via Tel: (+234)-806-225-7480 | Email: abubakarzakari1703@gmail.com

Copywright@devmaesters.com
Privacy Policy

By using our website,
you agree that devmaesters can store cookies on your device and disclose information in accordance with our privacy policy.