W
Wasp•3mo ago
Andres Montalban

How can I create a JSON field in a model?

Hi, I'm trying to create a model that has a JSON field (Mainly to store a structured data) but I'm facing an error that I can't figure it out. Here are the models:
model User {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
email String @unique
emailVerified Boolean @default(false)
googleId String @unique
displayName String
firstName String
lastName String
profilePicture String
receiveMarketingEmails Boolean @default(false)
QRCodes QRCode[]
quota Int @default(10)
}

model QRCode {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
user User? @relation(fields: [userId], references: [id])
userId String @db.Uuid
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
key String @unique
redirectURL String?
isActive Boolean @default(false)
description String?
Scans Scan[]
scansCount Int @default(0)
}

model Scan {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
qrCode QRCode? @relation(fields: [qrCodeId], references: [id])
qrCodeId String @db.Uuid
createdAt DateTime @default(now())
geoLocationData Json?
}
model User {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
email String @unique
emailVerified Boolean @default(false)
googleId String @unique
displayName String
firstName String
lastName String
profilePicture String
receiveMarketingEmails Boolean @default(false)
QRCodes QRCode[]
quota Int @default(10)
}

model QRCode {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
user User? @relation(fields: [userId], references: [id])
userId String @db.Uuid
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
key String @unique
redirectURL String?
isActive Boolean @default(false)
description String?
Scans Scan[]
scansCount Int @default(0)
}

model Scan {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
qrCode QRCode? @relation(fields: [qrCodeId], references: [id])
qrCodeId String @db.Uuid
createdAt DateTime @default(now())
geoLocationData Json?
}
When I try to start Wasp I get the following error. If I do not create the FK between Scan and QRCode it works fine. Thanks in advance!
9 Replies
kapa.ai
kapa.ai•3mo ago
Hi @Andres Montalban. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the provided models, it seems like you are trying to use a JSON field (geoLocationData) in your Scan model. The error message is not provided, so it's hard to pinpoint the exact issue. However, Wasp uses Prisma under the hood to interact with the database, and Prisma does support JSON fields. If you're having trouble with the JSON field, it might be due to the database you're using. Not all databases support JSON fields. For example, SQLite doesn't support JSON fields, while PostgreSQL does. If you're using a database that supports JSON fields and you're still encountering an issue, it might be due to a Prisma-related issue. You can check the Prisma documentation (https://www.prisma.io/docs/) for more information on how to use JSON fields. If you're still having trouble, you might want to check the Wasp community (https://discord.com/channels/686873244791210014) or Wasp Github issues (https://github.com/wasp-lang/wasp/issues) for similar problems or to ask for help. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Andres Montalban, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Andres Montalban
Andres MontalbanOP•3mo ago
@kapa.ai sorry I provided the error message. I am using PostgreSQL as backend database.
kapa.ai
kapa.ai•3mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I'm sorry, but without the error message, it's difficult to provide a specific solution to your problem. However, since you're using PostgreSQL, which supports JSON fields, the issue might be related to how you're using the JSON field in your code. If you're trying to insert or update data in the JSON field, make sure you're providing a valid JSON object. If you're querying data, ensure you're using the correct Prisma methods for JSON fields. If the issue persists, I recommend checking the Prisma documentation (https://www.prisma.io/docs/) for more information on how to use JSON fields with PostgreSQL. You can also reach out to the Wasp community (https://discord.com/channels/686873244791210014) or Wasp Github issues (https://github.com/wasp-lang/wasp/issues) for more help. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Andres Montalban, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
martinsos
martinsos•3mo ago
@miho @sodic will be able to shed some light on this potentially. In the meantime @Andres Montalban : where is that error coming from exactly? server/crud/QRCodes.ts -> are you using the wasp's CRUD functionality? What if you don't use Crud but instead implement your own Queries and Actions, have you given that a try maybe?
Andres Montalban
Andres MontalbanOP•3mo ago
Thank you @martinsos I will give it a try to Queries and Actions and report back
miho
miho•3mo ago
Which Wasp version are you using? 🙂 I'm not sure if we support JSON fields properly in our serialisation layer @sodic
Created an issue: https://github.com/wasp-lang/wasp/issues/2348
Filip
Filip•3mo ago
Yeah, we most likely don't. I never tried using them Thanks for making the issue @miho @Andres Montalban Any luck when switching to Queries/Actions
Andres Montalban
Andres MontalbanOP•2mo ago
Hey, sorry for the delay. I ended up using an action for what I needed but had to create the DB fields that were present in the JSON in order to unblock me. Thanks!
MEE6
MEE6•2mo ago
Wohooo @Andres Montalban, you just became a Waspeteer level 1!
Want results from more Discord servers?
Add your server