Migrate: Fields get wiped out on running a migration

Created on 23 Dec 2019  ·  8Comments  ·  Source: prisma/migrate

Data get deleted even there has been no change made to the schema. This is not the expected behavior.

How to reproduce

1) Run prisma2 dev migration to create the database (Schema below)
2) Run seed script attached below to fill it with data
3) Run prisma2 lift save
This will prompt a warning

⚠️  There might be data loss when applying the migration:

  • You are about to alter the column `length` on the `File` table, which still contains 2 non-null values. The data in that column will be lost.
  • You are about to alter the column `preview` on the `File` table, which still contains 2 non-null values. The data in that column will be lost.
  • You are about to alter the column `password` on the `User` table, which still contains 1 non-null values. The data in that column will be lost.
  • You are about to alter the column `username` on the `User` table, which still contains 1 non-null values. The data in that column will be lost.

4) Run prisma2 lift up. This will also show a warning and after confirming it, it deletes the data.

Data before migration (User model):
Screenshot 2019-12-23 at 23 25 53

Data after migration (User model):
Screenshot 2019-12-23 at 23 26 24

Schema

datasource db {
  provider = "postgresql"
  url      = env("POSTGRES_URL")
}

generator photon {
  provider = "photonjs"
}

model File {
  id      String  @id @default(cuid())
  name    String
  project Project
  url     String
  type    String
}

model User {
  id            String        @id @default(cuid())
  authId        String?       @unique
  email         String        @unique
  projects      Project[]
  projectsAdmin Project[]     @relation(name: "admins")
  name          String
  profileImg    String?
  postVersions  PostVersion[]
  faqs          Faq[]
  createdAt     DateTime      @default(now())
  updatedAt     DateTime      @updatedAt
}

model Company {
  id       String    @id @default(cuid())
  name     String
  projects Project[]
}

model Project {
  id                   String         @id @default(cuid())
  name                 String
  company              Company
  webUrl               String?
  logo                 String
  logoSmall            String
  postCategories       PostCategory[]
  faqCategories        FaqCategory[]
  posts                PostVersion[]
  faqs                 Faq[]
  users                User[]
  admins               User[]         @relation(name: "admins")
  files                File[]
  faqFeature           Boolean
  blogFeature          Boolean
  sectionFeature       Boolean
  establishmentFeature Boolean
  supplierFeature      Boolean
  languages            String
  editorConfig         String
}

model Post {
  id               String      @id @default(cuid())
  draftedVersion   PostVersion @relation(name: "draft")
  publishedVersion PostVersion @relation(name: "published")
}

model PostVersion {
  id            String         @id @default(cuid())
  draftedPost   Post?          @relation(name: "draft")
  publishedPost Post?          @relation(name: "published")
  title         String
  slug          String
  text          String
  language      String
  categories    PostCategory[]
  mainImg       String
  author        User
  published     Boolean
  project       Project
  ogTitle       String?
  ogDescription String?
  tags          String?
  createdAt     DateTime
  updatedAt     DateTime       @updatedAt
}

model Faq {
  id             String       @id @default(cuid())
  question       String
  answer         String
  category       FaqCategory?
  published      Boolean
  language       String
  project        Project
  author         User
  largeLinkTitle String?
  largeLink      String?
  createdAt      DateTime     @default(now())
  updatedAt      DateTime     @updatedAt
}

model PostCategory {
  id      String        @id @default(cuid())
  name    String
  project Project
  posts   PostVersion[]
}

model FaqCategory {
  id      String  @id @default(cuid())
  name    String
  project Project
  faqs    Faq[]
}

model Section {
  id      String        @id @default(cuid())
  project Project
  title   String
  items   SectionItem[] @relation(onDelete: CASCADE)
}

model SectionItem {
  id          String @id @default(cuid())
  amount      Int
  description String
}

model Establishment {
  id             String          @id @default(cuid())
  project        Project
  name           String
  country        String?
  street         String?
  city           String?
  postalCode     String?
  morningHours   BusinessHours[] @relation(name: "morning")
  afternoonHours BusinessHours[] @relation(name: "afternoon")
  departments    Department[]
  email          String?
  phone          String?
}

model BusinessHours {
  id        String         @id @default(cuid())
  morning   Establishment? @relation(name: "morning")
  afternoon Establishment? @relation(name: "afternoon")
  day       Int
  openTime  DateTime?
  closeTime DateTime?
}

model Department {
  id     String             @id @default(cuid())
  name   String
  people DepartmentPeople[]
}

model DepartmentPeople {
  id       String  @id @default(cuid())
  name     String
  jobTitle String?
  phone    String?
  email    String?
}

model SupplierCategory {
  id          String                @id @default(cuid())
  project     Project
  name        String
  subCategory SupplierSubCategory[]
}

model SupplierSubCategory {
  id        String     @id @default(cuid())
  name      String
  suppliers Supplier[]
}

model Supplier {
  id   String @id @default(cuid())
  name String
  logo String
}

Seed

import { Photon } from "@prisma/photon"
import bcrypt from "bcrypt"
const photon = new Photon()

export const seed = async () => {
    const user = await photon.users.create({
        data: {
            username: "admin",
            password: bcrypt.hashSync("admin", 12),
            role: "ADMIN",
            notificationSettings: {
                create: {}
            }
        }
    })
    const lecture = await photon.lectures.create({
        data: {
            status: "APPROVED",
            index: 0,
            name: "Jak vznikly peníze",
            description:
                "Knowledge is a familiarity, awareness, or understanding of someone or something, such as facts, information, descriptions, or skills, which is acquired through experience or education by perceiving, discovering, or learning",
            files: {
                create: [
                    {
                        type: "VIDEO",
                        src: "https://storage.googleapis.com/filip-csob-testing/uvod.mp4",
                        length: 94.549333,
                        preview:
                            "https://storage.googleapis.com/filip-csob-testing/video_preview.png"
                    },
                    {
                        type: "AUDIO",
                        src: "https://audio.com",
                        length: 94.549333,
                        preview:
                            "https://storage.googleapis.com/filip-csob-testing/video_preview.png"
                    }
                ]
            },
            summary: "This is a summary",
            transcript: "This is a transcript",
            author: {
                connect: {
                    id: user.id
                }
            },
            chapter: {
                create: {
                    status: "APPROVED",
                    color: "#04AE1C",
                    index: 0,
                    icon: {
                        create: {
                            svg:
                                "https://storage.googleapis.com/filip-csob-testing/chapter_icon.svg",
                            png:
                                "https://storage.googleapis.com/filip-csob-testing/chapter_icon.png"
                        }
                    },
                    name: "Peníze",
                    totalLength: 1260,
                    description: "Peníze jsou důležité a tohle je důležitý popis",
                    author: {
                        connect: {
                            id: user.id
                        }
                    }
                }
            }
        }
    })

    const securityQuestions = await photon.securityQuestions.create({
        data: {
            text: "Jak se jmenuješ?"
        }
    })

    const test = await photon.tests.create({
        data: {
            lecture: {
                connect: {
                    id: lecture.id
                }
            },
            maxPoints: 40,
            questions: {
                create: [
                    {
                        text: "Co musíš udělat jako první, když ztratíš platební kartu?",
                        type: "SELECT",
                        description:
                            "V případě, že ztratíš svou platební kartu je vždy nutné požádat o okamžité zablokování. Pokud bys tak neučinil, tak může dojít ke zneužití karty a můžeš přijít o své peníze.",
                        answers: {
                            create: [
                                {
                                    isCorrect: false,
                                    points: 0,
                                    text: "Pořídíš si ihned novou kartu"
                                },
                                {
                                    text: "Budeš nakupovat za hotové",
                                    isCorrect: false,
                                    points: 0
                                },
                                {
                                    text: "Požádáš banku o zablokování karty",
                                    points: 10,
                                    isCorrect: true
                                }
                            ]
                        }
                    },
                    {
                        text: "Co musíš udělat jako první, když ztratíš peněženku?",
                        type: "SELECT",
                        description: "V případě, že ztratíš svou peněžeku, nahlaš to na policii.",
                        answers: {
                            create: [
                                {
                                    text: "Pořídíš si ihned novou peněženku",
                                    isCorrect: false,
                                    points: 0
                                },
                                {
                                    text: "Budeš nakupovat přes Apple Pay.",
                                    isCorrect: false,
                                    points: 0
                                },
                                {
                                    text: "Zavoláš na policii a nahlásíš ztrátu.",
                                    points: 10,
                                    isCorrect: true
                                }
                            ]
                        }
                    },
                    {
                        text: "Co musíš udělat jako první, když upadneš?",
                        type: "SELECT",
                        description: "Můžeš i ležet, ale asi to nebude vypadat dobře.",
                        answers: {
                            create: [
                                {
                                    isCorrect: false,
                                    points: 0,
                                    text: "Zakopeš se"
                                },
                                {
                                    text: "Budeš ležet",
                                    isCorrect: false,
                                    points: 0
                                },
                                {
                                    text: "Zvedneš se",
                                    points: 10,
                                    isCorrect: true
                                }
                            ]
                        }
                    },
                    {
                        text: "Kolik je 1+1?",
                        type: "SELECT",
                        description: "Jedna a jedna je vždy dva",
                        answers: {
                            create: [
                                {
                                    isCorrect: false,
                                    points: 0,
                                    text: "8"
                                },
                                {
                                    text: "2",
                                    isCorrect: true,
                                    points: 10
                                },
                                {
                                    text: "150",
                                    points: 0,
                                    isCorrect: true
                                }
                            ]
                        }
                    }
                ]
            }
        }
    })

    process.exit()
}

seed()

Most helpful comment

I was talking about this issue with Tom on monday in the previous issue mentioned above. I expect data will not get deleted as I have not done any change to the schema.

All 8 comments

@tomhoule when do you think you get to this issue? We use Prisma in staging now and it is not working so I would have to downgrade Prisma version to make it work. Thank you!

No need to mention our developers here @malekjaroslav, we will get to it when we get to it: First someone will try to triage and reproduce the issue, then we will see the bigger context of the problem and then decide where this lands on our internal priority list.

That being said, you did not mention what you actually expect to happen here. Is the "There might be data loss..." wrong? Or the reason it mentions? What should be different in a "fixed" version?

I was talking about this issue with Tom on monday in the previous issue mentioned above. I expect data will not get deleted as I have not done any change to the schema.

It looks like the last edits in prisma-engine fixed the issue. I can no longer replicate it in Prisma2 alpha.482

Can we get an update on when we can expect a release to address the problem? This issue is clearly affecting a large number of people and makes the framework inoperable for anything beyond a trivial use case.

This is a 23-day old breaking issue. "We will get to it when we get to it" does not inspire confidence in those looking to adopt the Prisma Framework.

Related issues:
https://github.com/prisma/lift/issues/274
https://github.com/prisma/lift/issues/303
https://github.com/prisma/lift/issues/279
https://github.com/prisma/lift/issues/276
https://github.com/prisma/lift/issues/278

I am fairly confident this issue was fixed a few weeks ago (https://github.com/prisma/prisma-engine/pull/326 and others), but we haven't released a preview version since. Please try again with the latest prisma2@alpha or wait for the next preview release, which should be very soon now.

edit: I tried with the latest alpha and I can't reproduce the problem.

Confirming that upgrading to prisma2@alpha solved this problem for me.

Closing this now, feel free to comment here if you are still encountering this.

Was this page helpful?
0 / 5 - 0 ratings