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

Unable to use native database types with Prisma and CockroachDB #14176

Closed
harshhhdev opened this issue Jul 6, 2022 · 5 comments
Closed

Unable to use native database types with Prisma and CockroachDB #14176

harshhhdev opened this issue Jul 6, 2022 · 5 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: cockroachdb topic: native database types
Milestone

Comments

@harshhhdev
Copy link

harshhhdev commented Jul 6, 2022

Bug description

I had opened my Prisma schema on a new machine with my editor configured. The schema I was using is attached below.

You can ignore most of the schema -- the only important part is the Question model as the type of the content model was VARCHAR(100). The Prisma extension gave me an error, which said Invalid Native type VarChar.

This seems like something which has happened after I updated Prisma to v4.0.0 as everything was working fine before. If I remove the @db.VarChar(100) and keep it to just a string, I'm unable to run the migration because it changes the type of the column.

How to reproduce

  1. Create a new Prisma schema with provider set to 'CockroachDB'
  2. Create a model
  3. Mark a field of type String with @db.VarChar(100)
  4. See error

Expected behavior

I would expect that I can use the native database type @db.VarChar by default without any errors, as in the documentation about 'Supported database types and features' it clearly shows us that we can use it.

Prisma information

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

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

enum Status {
  ANSWERED
  UNANSWERED
}

model Question {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now())
  status    Status   @default(UNANSWERED)
  content   String   @db.VarChar(100)
  answer    String?
}

model Link {
  id     String @id @default(uuid())
  slug   String @unique
  url    String
  userId String
  user   User   @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model Account {
  id                       String   @id @default(cuid())
  createdAt                DateTime @default(now())
  updatedAt                DateTime @updatedAt
  userId                   String
  type                     String
  provider                 String
  providerAccountId        String
  refresh_token            String?
  refresh_token_expires_in Int?
  access_token             String?
  expires_at               Int?
  token_type               String?
  scope                    String?
  id_token                 String?
  session_state            String?
  oauth_token_secret       String?
  oauth_token              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())
  createdAt     DateTime  @default(now())
  updatedAt     DateTime  @updatedAt
  isAdmin       Boolean   @default(false)
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  links         Link[]
}

model VerificationToken {
  identifier String
  token      String   @unique
  expires    DateTime

  @@unique([identifier, token])
}

Environment & setup

  • OS: I was running this on GitHub Codespaces in the browser
  • Database: CockroachDB
  • Node.js version: v16.14.2

Prisma Version

Note, this is inside of a GitHub Codespace

@harshhhdev ➜ /workspaces/harshhhdev.github.io (master ✗) $ yarn prisma -v
yarn run v1.22.15
$ /workspaces/harshhhdev.github.io/node_modules/.bin/prisma -v
Environment variables loaded from .env
prisma                  : 4.0.0
@prisma/client          : 4.0.0
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : da41d2bb3406da22087b849f0e911199ba4fbf11
Studio                  : 0.465.0
@harshhhdev harshhhdev added the kind/bug A reported bug. label Jul 6, 2022
@harshhhdev harshhhdev changed the title Unable to use native database types with Prisma Unable to use native database types with Prisma and CockroachDB Jul 6, 2022
@harshhhdev
Copy link
Author

I copy pasted the schema shown in the 'supported database types and features' example, and it worked without an error:

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

model User {
  id    Int    @id @default(autoincrement())
  name  String @db.VarChar(200)
  posts Post[]
}

model Post {
  id        Int     @id @default(autoincrement())
  title     String  @db.VarChar(150)
  published Boolean @default(true)
  authorId  Int
  author    User    @relation(fields: [authorId], references: [id])
}

However, when I switched the provider from postgresql to cockroachdb, that's when I got the error. This seems to be a new error after support for CockroachDB was GA, as back when it was a preview feature I didn't face this issue.

@janpio
Copy link
Member

janpio commented Jul 6, 2022

On CockroachDB, you can use @db.String to achieve what you want. I fear our documentation is not up to date here, as is the auto completion of our VSCode extension.

Misleading suggestion:
misleading suggestion

Following error message:
invalid native type

https://www.prisma.io/docs/concepts/database-connectors/cockroachdb#type-mapping-limitations-in-cockroachdb seems to be misleading and skip over the differences in the native types.

@janpio janpio added bug/2-confirmed Bug has been reproduced and confirmed. topic: native database types team/schema Issue for team Schema. topic: cockroachdb labels Jul 6, 2022
@harshhhdev
Copy link
Author

Hey @janpio,

Thank you for getting back to me quickly 😄. Using @db.String solved the issue. There was another reference of the native database type of STRING being mapped to @db.String here, however I do think it'd it's necessary that it should be mentioned in the type mapping limitations section. Could I send a quick PR to fix the documentation?

@janpio
Copy link
Member

janpio commented Jul 7, 2022

You can, but we are also trying to figure out what the actual differences are - most probably there are more than just VarChar => String. We also need to fix our auto completion and everything.

@tomhoule
Copy link
Contributor

tomhoule commented Jul 7, 2022

VarChar is not a valid native type on cockroachdb but autocompletion was not reflecting that. We fixed this in prisma/prisma-engines#3044 — thanks for reporting the issue!

@tomhoule tomhoule closed this as completed Jul 7, 2022
@janpio janpio added this to the 4.1.0 milestone Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: cockroachdb topic: native database types
Projects
None yet
Development

No branches or pull requests

3 participants