prisma / migrate

Issues for Prisma Migrate are now tracked at prisma/prisma. This repo was used to track issues for Prisma Migrate Experimental and is now deprecated.
https://www.prisma.io/docs/concepts/components/prisma-migrate
Apache License 2.0
767 stars 22 forks source link

Postgres Migration Error: error: Error querying the database: Error querying the database: Error querying the database: db error: ERROR: syntax error at or near " ;" #421

Closed austincollinpena closed 4 years ago

austincollinpena commented 4 years ago

report id 3574

Bug description

When I run migrations, I get an error stating:

Failure during a migration command: Connector error. (error: Error querying the database: Error querying the database: Error querying the database: db error: ERROR: syntax error at or near "
;")

However, if I remove my two migration files and run it again (kept in the repo so this step too could be reproduced), I get the same error, but this time my database gets updated.

How to reproduce

I made a repo, all that needs to be done is to run the migrations. Repo here

In order to reproduce, you must first run the first migration, add several fields of data to the "Link" model with no relations (There is a mutation set up already for this), and then run the next mutations.

Key Point: I bet my schema below is not set up correctly for non nullable fields. Because I already had 4 records and tried to add relational fields, I bet that caused the issue. How should I structure my schema to avoid this and allow nullable relation fields?

When I dropped my tables and re-ran migrations all went well.

Expected behavior

I expect the migrations to work with no errors.

Prisma information

Can be seen here

And schema below (issue is likely here)

generator client {
  provider = "prisma-client-js"
}

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

model Link {
  createdAt   DateTime @default(now())
  description String
  id          Int      @default(autoincrement()) @id
  url         String
  user        User?    @relation(fields: [userId], references: [id])
  userId      Int?     @unique
}

model User {
  createdAt     DateTime               @default(now())
  email         String                 @unique
  id            Int                    @default(autoincrement()) @id
  password      String
  links         Link[]
  categories    RememberItemCategory[]
  rememberItems RememberItems[]
}

model Profile {
  id     Int  @default(autoincrement()) @id
  user   User @relation(fields: [userId], references: [id])
  userId Int  @unique
}

model RememberItemCategory {
  id       Int    @default(autoincrement()) @id
  user     User   @relation(fields: [userId], references: [id])
  userId   Int    @unique
  category String
}

model RememberItems {
  id        Int      @default(autoincrement()) @id
  createdAt DateTime @default(now())
  userId    Int      @unique
  user      User     @relation(fields: [userId], references: [id])
}

Environment & setup

yarn run v1.22.4
$ /home/austin/programming-projects/react/minimum-reproduceable-query-issue/node_modules/.bin/prisma migrate up --experimental
Environment variables loaded from ./prisma/.env
🏋️‍  migrate up

Changes to be applied:

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

model Link {
  createdAt DateTime @default(now())
  description String
  id Int @default(autoincrement()) @id
  url String
  user User? @relation(fields: [userId], references: [id])
  userId Int? @unique
}

model User {
  createdAt DateTime @default(now())
  email String @unique
  id Int @default(autoincrement()) @id
  password String
  links Link[]
  categories RememberItemCategory[]
  rememberItems RememberItems[]
}
**// Link Model Was highlighted as red**
model Link {
    id Int @id @default(autoincrement())
    createdAt DateTime @default(now())
    description String
    url String
model Profile {
  id Int @default(autoincrement()) @id
  user User @relation(fields: [userId], references: [id])
  userId Int @unique
}
model RememberItemCategory {
  id Int @default(autoincrement()) @id
  user User @relation(fields: [userId], references: [id])
  userId Int @unique
  category String
}

model RememberItems {
  id Int @default(autoincrement()) @id
  createdAt DateTime @default(now())
  userId Int @unique
  user User @relation(fields: [userId], references: [id])
}

Checking the datasource for potential data loss...

Database Changes:

Migration                                 Database actions           Status

20200413224532-added-user                  statements.               
20200413225543-adding-remember-and-items  2 CreateTable statements.  

You can get the detailed db changes with prisma migrate up --experimental --verbose
Or read about them in the ./migrations/MIGRATION_ID/README.md
 ERROR  Oops, an unexpected error occured!
Failure during a migration command: Connector error. (error: Error querying the database: Error querying the database: Error querying the database: db error: ERROR: syntax error at or near "
;")
tomhoule commented 4 years ago

Thanks for reporting this bug! looking at the generated SQL in the migrations folder, it looks like it's the same issue as https://github.com/prisma/migrate/issues/365 - it should be fixed on alpha already, but another PR is on its way with a more general solution.