db migrate-dev removes models in schema.prisma

why would wasp db migrate-dev remove models I've added to schema.prisma?
13 Replies
kapa.ai
kapa.ai4w ago
Hi @enlightenmental. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
MEE6
MEE64w ago
Wohooo @enlightenmental, you just became a Waspeteer level 2!
kapa.ai
kapa.ai4w ago
---------------------- The issue you're experiencing with wasp db migrate-dev removing models from your schema.prisma file is likely related to how Wasp generates the Prisma schema. In Wasp, you should define your entities in the main.wasp file using the entity declaration, not by directly modifying the schema.prisma file. When you run wasp db migrate-dev, Wasp regenerates the schema.prisma file based on the entities defined in your main.wasp file, which would overwrite any manual changes you made to schema.prisma. This is because Wasp manages the Prisma schema for you based on your entity definitions in the Wasp language. If you've been adding models directly to schema.prisma instead of defining them as entities in main.wasp, they will be removed when Wasp regenerates the schema. To properly add models to your database schema, you should define them as entities in your main.wasp file like this:
entity MyEntity {=psl
id Int @id @default(autoincrement())
name String
// other fields
psl=}
entity MyEntity {=psl
id Int @id @default(autoincrement())
name String
// other fields
psl=}
Then run wasp db migrate-dev to create the migration and update your database schema. If you're experiencing issues with certain fields not being recognized even when defined in your entity, this might be related to a known issue where the position of fields within the entity definition can affect whether they're properly recognized Improve handling of invalid Prisma schemas. In such cases, try moving the problematic fields to the top of your entity definition. After making changes to your entities, always run wasp db migrate-dev to synchronize your database with the current state of your schema. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @enlightenmental, 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?
enlightenmental
enlightenmentalOP4w ago
@kapa.ai can you provide a full example of an entity definition in main.wasp?
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Here's a full example of an entity definition in main.wasp:
entity User {=psl
id Int @id @default(autoincrement())
recipes Recipe[]
psl=}

entity Recipe {=psl
id Int @id @default(autoincrement())
title String
description String?
userId Int
user User @relation(fields: [userId], references: [id])
psl=}
entity User {=psl
id Int @id @default(autoincrement())
recipes Recipe[]
psl=}

entity Recipe {=psl
id Int @id @default(autoincrement())
title String
description String?
userId Int
user User @relation(fields: [userId], references: [id])
psl=}
This example defines two entities: User and Recipe. The User entity has an ID field and a relation to multiple recipes. The Recipe entity has an ID, title, optional description, and a relation back to the user who created it. Entity definitions in Wasp use Prisma Schema Language (PSL) between the {=psl psl=} tags. The minimum requirement for an entity is to have an ID field:
entity Task {=psl
id Int @id @default(autoincrement())
description String
isDone Boolean @default(false)
psl=}
entity Task {=psl
id Int @id @default(autoincrement())
description String
isDone Boolean @default(false)
psl=}
After defining entities in your main.wasp file, you need to run wasp db migrate-dev to create the database migration and apply it to your database Entities documentation. You can then view your entities in the database by running wasp db studio Tutorial documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @enlightenmental, 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?
enlightenmental
enlightenmentalOP4w ago
@kapa.ai ^ see last message?
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Yes, I see my last message where I provided a full example of entity definitions in main.wasp. I showed how to define User and Recipe entities with relations between them, as well as a simpler Task entity example. Is there something specific about entity definitions that you'd like me to clarify or expand upon? Or perhaps you're looking for a different type of entity example with other features like additional field types or relation types? ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @enlightenmental, 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?
enlightenmental
enlightenmentalOP4w ago
@kapa.ai schema.prisma enum EmailStatus { unknown safe invalid disabled disposable inbox_full catch_all role_account spamtrap } then running migrate-dev returns [ Db ] [-] Removed enums [ Db ] - EmailStatus
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I see the issue you're encountering. When you add an enum directly to the schema.prisma file and then run wasp db migrate-dev, Wasp is removing your enum definition. This happens because Wasp currently doesn't fully support Prisma enums in its entity system. According to the knowledge sources, enum support is a planned feature but not yet implemented:
It's not possible yet, Here is a Github issue to track that https://github.com/wasp-lang/wasp/issues/641
Expand our Prisma support (enum, type, other dbs, ...) When you run wasp db migrate-dev, Wasp regenerates the schema.prisma file based on your entity definitions in main.wasp, which overwrites any manual changes you made directly to the schema.prisma file, including your enum definitions. As a workaround, one user found a solution by: 1. Creating a seeder function that uses $executeRawUnsafe to create the enum in the database 2. Upgrading to Prisma 5.10.2 (as the version shipped with Wasp crashes Prisma Studio if an enum is defined in the db)
Just got a workaround fully working for Postgress
1) Created a seeder function that use `$executeRawUnsafe` to create the enum.
2) Upgraded to prisma `5.10.2`. Version shipped with wasp crashes Prisma Studio if an enum is defined in the db
Just got a workaround fully working for Postgress
1) Created a seeder function that use `$executeRawUnsafe` to create the enum.
2) Upgraded to prisma `5.10.2`. Version shipped with wasp crashes Prisma Studio if an enum is defined in the db
Discord support thread: Prisma Enum However, be cautious with this approach as it's a hack and might cause other issues since Wasp is designed to work with Prisma 4, not Prisma 5. The Wasp team has indicated that better Prisma support, including enums, is planned for future releases. Is there a specific use case you're trying to solve with the enum? I might be able to suggest an alternative approach that works with the current version of Wasp. GitHub: Vinay Sharma | Twitter: @vinay_sharmaa | Website: vinaysharma.io ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @enlightenmental, 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?
enlightenmental
enlightenmentalOP4w ago
@kapa.ai I don't think you're referencing V0.16.0 docs..
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- You're right, and I apologize for the confusion. The information I provided was based on older documentation and issues. Looking at the knowledge sources provided, I don't see specific information about Prisma enum support in Wasp v0.16.0. The most recent information in the sources indicates that enum support in Wasp is still a planned feature tracked in this GitHub issue, but doesn't mention if it's been implemented in v0.16.0. There are several discussions about Prisma-related issues and workarounds in the knowledge sources, but none specifically confirming enum support in the latest version. If you're using Wasp v0.16.0 and experiencing issues with Prisma enums, I'd recommend: 1. Checking the official Wasp documentation for v0.16.0 2. Looking at the release notes for v0.16.0 to see if enum support was added 3. Trying the workaround mentioned in the Discord thread if enums are still not supported Would you like me to help you find more up-to-date information about Prisma enum support in Wasp v0.16.0? ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @enlightenmental, 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?
Carlos
Carlos4w ago
Hey @enlightenmental are you sure it is removing the enum from your model? Prisma's output during migration generation is told from the perspective of "this enum is not in the db the migrations would generate", that's why it says "Deleted". Can you check on the migration files? I suspect it is creating it and working fine.
enlightenmental
enlightenmentalOP4w ago
@Carlos I confirmed it is NOT removing - the migrate-dev command failed because the app has code issues - so it was not able to get far enough to update the database scheme. (false alarm) I assumed db migrate-dev only updated the database and did not require the app to build.

Did you find this page helpful?