wasp-lang / wasp

The fastest way to develop full-stack web apps with React & Node.js.
https://wasp-lang.dev
MIT License
13.49k stars 1.18k forks source link

Prisma support for dbgenerated UUID field #2137

Closed amontalban closed 3 months ago

amontalban commented 3 months ago

Describe the bug Hey team,

I have started testing OpenSaaS and I was playing with UUID fields. Given my background (DBA exposure) I wanted to use native UUID fields in the DB and I stumbled upon the Prisma Doc on how to do this.

Therefore I tried to use it in the entity but it doesn't work as it seems to be trimmed in the schema.prisma file:

model User {
  id String @id @default(dbgenerated("gen_random_uuid()
  auth Auth?

}

Thank you in advance!

To Reproduce

Create an entity like the following:

entity User {=psl  // Data models are defined using Prisma Schema Language.
  id                        String               @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  first_name                String?
  last_name                 String?
  email                     String               @unique
psl=}

Expected behavior

The expected output should be (As shown in doc):

model User {
  id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
}

Screenshots When I run wasp db migrate-dev I get:

Screenshot 2024-06-30 at 11 55 13
infomiho commented 3 months ago

@amontalban yep, it seems like we have a bug in our current Prisma file parser!

Thank you for the report. I do have some good news :) we already upgraded the parser for the next 0.14.0 version of Wasp and I've added your case to our list of tests in the new implementation.

We expect to release Wasp 0.14.0 mid-July. I hope you can use the uuid() approach until then 🤞

amontalban commented 3 months ago

@amontalban yep, it seems like we have a bug in our current Prisma file parser!

Thank you for the report. I do have some good news :) we already upgraded the parser for the next 0.14.0 version of Wasp and I've added your case to our list of tests in the new implementation.

We expect to release Wasp 0.14.0 mid-July. I hope you can use the uuid() approach until then 🤞

Wow that's great news! Thank you very much, looking forward for 0.14.0 release 🚀

amontalban commented 2 months ago

@infomiho just a follow up question, I have been using v0.14.0-rc1 to test this fix and given is my first time using Wasp I'm a total newbie with it.

When I run wasp db migrate-dev I get this error:

ERROR: foreign key constraint "Auth_userId_fkey" cannot be implemented
DETAIL: Key columns "userId" and "id" are of incompatible types: text and uuid.

Checking the migration looks like the Auth model uses TEXT field for userId as shown in docs and based on my research this cannot be modified.

Therefore, looks like the id of the User model needs to be Int, right? If so, maybe it would be a good idea to enforce it?

Thanks in advance for your help!

infomiho commented 2 months ago

Hey @amontalban, we are parsing your User model and using the type of the User’s ID field in the Auth model, so this should work fine out of the box with Int or String.

Could you maybe share your schema.prisma file here so I can test it out myself? Maybe we have a bug on our hands.

amontalban commented 2 months ago

Hey @amontalban, we are parsing your User model and using the type of the User’s ID field in the Auth model, so this should work fine out of the box with Int or String.

Could you maybe share your schema.prisma file here so I can test it out myself? Maybe we have a bug on our hands.

@infomiho Yes, I think the problem is that I'm using UUID and not Int or String. Let me know if you would like me to open a new issue with this.

Here are the snippets:

datasource db {
  provider = "postgresql"
  // Wasp requires that the url is set to the DATABASE_URL environment variable.
  url      = env("DATABASE_URL")
}

// Wasp requires the `prisma-client-js` generator to be present.
generator client {
  provider = "prisma-client-js"
}

model User {
  id                String       @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  createdAt         DateTime     @default(now())
  updatedAt         DateTime     @default(now())
  email             String       @unique
  firstName         String
  lastName          String
  phone             String       @unique
}

And this is the main.wasp:

app testUuid {
  wasp: {
    version: "^0.14.0"
  },
  title: "test-uuid",
  auth: {
    // Tells Wasp which entity to use for storing users.
    userEntity: User,
    methods: {
      // Enable username and password auth.
      usernameAndPassword: {}
    },
    // We'll see how this is used in a bit.
    onAuthFailedRedirectTo: "/login"
  }
}

route RootRoute { path: "/", to: MainPage }
page MainPage {
  component: import { MainPage } from "@src/MainPage"
}
infomiho commented 2 months ago

Oh, you are right! We never account for the @db.Uuid annotation you set. This is definitely a Wasp bug, I've created a new issue to track it: https://github.com/wasp-lang/wasp/issues/2167

I'm not sure if we'll fix it right away for 0.14.0 or a follow up 0.14.1 version, but we'll fix it ASAP since it's blocking people from using native DB types for their User ID.

amontalban commented 2 months ago

Oh, you are right! We never account for the @db.Uuid annotation you set. This is definitely a Wasp bug, I've created a new issue to track it: #2167

I'm not sure if we'll fix it right away for 0.14.0 or a follow up 0.14.1 version, but we'll fix it ASAP since it's blocking people from using native DB types for their User ID.

Wow that's awesome, thank you very much!