user26630048
user26630048

Reputation: 1

Prisma Schema Migrate

I got a problem when learning to create a schematic model of a prism, as below

model User {
  id       Int       @id @default(autoincrement())
  username String
  email    String    @unique
  password String
  orders   Order[]
  products Product[]
}

model Product {
  id          Int     @id @default(autoincrement())
  name        String
  description String
  price       Int
  stock       Int
  userId      Int
  user        User    @relation(fields: [userId], references: [id])
  images      Image[]
  orders      Order[]
}

model Image {
  id        Int     @id @default(autoincrement())
  url       String
  productId Int
  product   Product @relation(fields: [productId], references: [id])
}

model Order {
  id         Int      @id @default(autoincrement())
  quantity   Int
  totalPrice Int
  orderDate  DateTime
  status     String

  userId Int
  user   User @relation(fields: [userId], references: [id])

  productId Int
  product   Product @relation(fields: [productId], references: [id])
}

when I run the command "npm prisma migrate dev" an error message appears in the terminal like this

pict an error message

What should I do?

I've tried googling and looking for solutions on YouTube. But I don't understand, and I don't have anyone to ask either.

Upvotes: 0

Views: 17

Answers (1)

Gedders
Gedders

Reputation: 13

In the error message it is saying that there are two rows currently in the table that this migration is going to cause a change to. The problem is that the new fields aren't marked as nullable, so when the update runs, your current data needs to be updated to make sure their new columns aren't null. This is leading to the error because prisma has no idea what to enter into those boxes, but has to enter something, and so fails.

Some examples but not an exhaustive list of how to fix this.

  1. Provide a default value for the new columns in your schema, examples of this here.
  2. By deleting the held data so the migration can run without needing to update any rows.
  3. You can also set these fields to nullable in the schema to avoid this error from throwing, examples in above link as well.

Upvotes: 0

Related Questions