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

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

Open
sohelzerdoumi opened this issue May 8, 2024 · 5 comments
Assignees
Labels
bug Something isn't working prisma shouldfix We should do/fix this at some point

Comments

@sohelzerdoumi
Copy link

sohelzerdoumi commented May 8, 2024

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

@sohelzerdoumi sohelzerdoumi changed the title Prefix entity with Prisma field type lead to incorrect schema Prefixing an entity with Prisma field type leads to an incorrect schema May 8, 2024
@sodic
Copy link
Contributor

sodic commented May 8, 2024

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

Nice work!

@infomiho infomiho added bug Something isn't working prisma shouldfix We should do/fix this at some point labels May 8, 2024
@Martinsos
Copy link
Member

Martinsos commented May 8, 2024

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!

@sodic
Copy link
Contributor

sodic commented May 8, 2024

@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
Copy link
Member

@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.

@infomiho infomiho self-assigned this May 24, 2024
@infomiho
Copy link
Contributor

This will be fixed by this commit 1b1753a#diff-55352e979a392d702b404e0a168abbf61a0c26527248deee8ea77055287e9626R85

Background: there is a reserved parser which does exactly what we need:

The lexeme parser reserved name parses symbol name, but it also checks that the name is not a prefix of a valid identifier. A reserved word is treated as a single token using try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working prisma shouldfix We should do/fix this at some point
Projects
None yet
Development

No branches or pull requests

4 participants