question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`db pull` gives different schema on CockroachDB compared with PostgreSQL

See original GitHub issue

Bug description

I’m writing a Getting Started guide for CockroachDB and I’m testing out creating a serverless cluster on Cockroach Cloud, adding some example SQL, and then using db pull to pull in the schema.

I’m using the same SQL as the PostgreSQL Getting Started example (see below), but when I pull I get a different Prisma schema to the one in the PostgreSQL example.

Raising an issue on @janpio’s recommendation so this can be investigated further.

How to reproduce

  1. Create a CockroachDB serverless cluster and connect to it
  2. Run the following SQL commands:
      CREATE TABLE "public"."User" (
      id SERIAL PRIMARY KEY NOT NULL,
      name VARCHAR(255),
      email VARCHAR(255) UNIQUE NOT NULL
    );
    
    CREATE TABLE "public"."Post" (
      id SERIAL PRIMARY KEY NOT NULL,
      title VARCHAR(255) NOT NULL,
      "createdAt" TIMESTAMP NOT NULL DEFAULT now(),
      content TEXT,
      published BOOLEAN NOT NULL DEFAULT false,
      "authorId" INTEGER NOT NULL,
      FOREIGN KEY ("authorId") REFERENCES "public"."User"(id)
    );
    
    CREATE TABLE "public"."Profile" (
      id SERIAL PRIMARY KEY NOT NULL,
      bio TEXT,
      "userId" INTEGER UNIQUE NOT NULL,
      FOREIGN KEY ("userId") REFERENCES "public"."User"(id)
    );
    
  3. Create a new Prisma project with the following schema file, and save the Cockroach connection string to the .env file:
    generator client {
      provider        = "prisma-client-js"
      previewFeatures = ["cockroachdb"]
    }
    
    datasource db {
      provider = "cockroachdb"
      url      = env("DATABASE_URL")
    }
    
  4. Run npx prisma db pull

This gave me the following schema:

model Post {
  id        BigInt   @id(map: "primary") @default(autoincrement())
  title     String   @db.VarChar(255)
  createdAt DateTime @default(now()) @db.Timestamp(6)
  content   String?
  published Boolean  @default(false)
  authorId  BigInt
  User      User     @relation(fields: [authorId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "fk_authorId_ref_User")
}

model Profile {
  id     BigInt  @id(map: "primary") @default(autoincrement())
  bio    String?
  userId BigInt  @unique
  User   User    @relation(fields: [userId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "fk_userId_ref_User")
}

model User {
  id      BigInt   @id(map: "primary") @default(autoincrement())
  name    String?  @db.VarChar(255)
  email   String   @unique @db.VarChar(255)
  Post    Post[]
  Profile Profile?
}

Expected behavior

I was expecting the schema from the PostgreSQL example instead:

model Post {
  id        Int      @id @default(autoincrement())
  title     String   @db.VarChar(255)
  createdAt DateTime @default(now()) @db.Timestamp(6)
  content   String?
  published Boolean  @default(false)
  authorId  Int
  User      User     @relation(fields: [authorId], references: [id])
}

model Profile {
  id     Int     @id @default(autoincrement())
  bio    String?
  userId Int     @unique
  User   User    @relation(fields: [userId], references: [id])
}

model User {
  id      Int      @id @default(autoincrement())
  name    String?  @db.VarChar(255)
  email   String   @unique @db.VarChar(255)
  Post    Post[]
  Profile Profile?
}

Prisma information

My full Prisma schema after pulling is:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["cockroachdb"]
}

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

model Post {
  id        BigInt   @id(map: "primary") @default(autoincrement())
  title     String   @db.VarChar(255)
  createdAt DateTime @default(now()) @db.Timestamp(6)
  content   String?
  published Boolean  @default(false)
  authorId  BigInt
  author    User     @relation(fields: [authorId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "fk_authorId_ref_User")
}

model Profile {
  id     BigInt  @id(map: "primary") @default(autoincrement())
  bio    String?
  userId BigInt  @unique
  user   User    @relation(fields: [userId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "fk_userId_ref_User")
}

model User {
  id      BigInt   @id(map: "primary") @default(autoincrement())
  name    String?  @db.VarChar(255)
  email   String   @unique @db.VarChar(255)
  posts   Post[]
  profile Profile?
}

Environment & setup

  • OS: Mac OS
  • Database: CockroachDB
  • Node.js version: v16.13.0

Prisma Version

3.8.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
janpiocommented, Jan 21, 2022

To be documented as limitations here: https://github.com/prisma/docs/pull/2737/files#r789828158

1reaction
janpiocommented, Jan 21, 2022

Still figuring that out really, in the context of the docs it probably is but not for our actual implementation fortunately.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Comparing CockroachDB and PostgreSQL
Let's unpack some of the architectural differences between PostgreSQL and CockroachDB to understand the challenges of the single server, ...
Read more >
Error on following the PostgreSQL 'Query your database ...
Follow steps in db pull gives different schema on CockroachDB compared with PostgreSQL #11158. Install and generate Prisma Client:.
Read more >
CockroachDB vs (MySQL, PostgreSQL, MongoDB & Cassandra)
A detailed overview of CockroackDB - features, architecture, advantages and comparing with MySQL, PostgreSQL, MongoDB & Cassandra.
Read more >
Prisma & CockroachDB | ORM for the cloud-distributed database
CockroachDB is a relational, PostgreSQL wire-protocol-compatible database built for cloud applications and services. It automates the task of scale, ...
Read more >
CockroachDB vs PostgreSQL | What are the differences?
CockroachDB - A distributed SQL database that scales fast, survives disaster, and thrives everywhere. PostgreSQL - A powerful, open source object-relational ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found