wasp-lang / wasp

The fastest way to develop full-stack web apps with React & Node.js.

Home Page:https://wasp-lang.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prefixing an entity with Prisma field type leads to an incorrect schema

sohelzerdoumi opened this issue · comments

When generating Prisma schema from Wasp entities, the reserved words for field type, collide with entity name. This results in incorrect Prisma schema output.

The following reserved words lead to incorrect replacement: String, Boolean, Int, BigInt, Float, Decimal, DateTime, Json, and Bytes

Example:

The following Wasp entity definition uses the reserved word Int as a prefix for an entity name Internet:

entity User {=psl
  id                        Int                  @id @default(autoincrement())
  // other...
  internets Internet[]       
psl=}

entity Internet {=psl
  id                        Int             @id @default(autoincrement())
  user                      User            @relation(fields: [userId], references: [id])
  userId                    Int
psl=}

The generated Prisma schema output is incorrect:

model User {
  id Int @id @default(autoincrement())
  internets Int
  auth Auth?

}
model Internet {
  id Int @id @default(autoincrement())
  user User @relation(fields: [userId], references: [id])
  userId Int

}

The expected output should be:

model User {
  id Int @id @default(autoincrement())
  internets Internet[]
  auth Auth?

}
model Internet {
  id Int @id @default(autoincrement())
  user User @relation(fields: [userId], references: [id])
  userId Int

}

Steps to reproduce:

  1. Create a Wasp entity definition file with a reserved word prefix (e.g. Int) for an entity name.
  2. Create a relation
  3. Run the Prisma schema generation command.
  4. Verify that the generated Prisma schema output is incorrect due to the reserved word prefix not being replaced.

Expected behavior: The reserved word prefix should be replaced with a valid entity name.

Probable issue location

pslFieldTypeToScalar fType = case fType of

Thanks for reporting @sohelzerdoumi. This is a significant bug.

Nice work!

Oh wow! This is a bug in our PSL parser -> I probably forgot to check for "end of word" when matching those keywords. Shouldn't be too hard to fix, a bit of parsec magic and we should be good!

@Martinsos The "end of word" rule possibly wouldn't solve it.

Not sure how Prisma handles this, but if PSL has different namespaces for type space and value space like most languages do, we'd need something more elaborate.

For example, if Prisma allows entities to be named Int, end-of-word wouldn't help us :)

@Martinsos The "end of word" rule possibly wouldn't solve it.

Not sure how Prisma handles this, but if PSL has different namespaces for type space and value space like most languages do, we'd need something more elaborate.

For example, if Prisma allows entities to be named Int, end-of-word wouldn't help us :)

INteresting point, but I doubt it, how would it differentiate between their Int and custom Int then? PSL is quite simple, so position is important, and at that position it always expects a type space name, not value space name. So I don't think this is an issue, it indeed should only be just "end of word". I am 98% confident in this.