Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prisma Migrate generates incorrect sql when chaining different types of table altering operations (remove map from @id, add native type to other field) #24331

Open
omaziarz opened this issue May 29, 2024 · 1 comment · May be fixed by prisma/prisma-engines#4996
Labels
bug/2-confirmed Bug has been reproduced and confirmed. domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/bug A reported bug. topic: broken migrations topic: migrate topic: postgresql topic: prisma migrate dev CLI: prisma migrate dev

Comments

@omaziarz
Copy link

omaziarz commented May 29, 2024

Bug description

If you perform two different types of alteration on a table for instance renaming a constraint and altering the data type of a column then prisma migrate dev will generate incorrect sql code.

How to reproduce

  1. Consider the following schema:
    model Fruits {
       id Int @id(map:"custom_name")
       name String @db.VarChar
    }
  2. edit it in this way :
    model Fruits {
       id Int @id()
       name String
    }
  3. run prisma migrate dev
  4. you will get the following error
    Database error code: 42601
    
    Database error:
    ERROR: syntax error at or near ","
    

This is because the generated sql will look like this:

ALTER TABLE "Fruits" RENAME CONSTRAINT "custom_name" TO "fruits_pkey",
ALTER COLUMN "name" SET DATA TYPE TEXT; -- this should be a separate ALTER TABLE statement --

Expected behavior

the generated sql should look like this :

ALTER TABLE "Fruits" RENAME CONSTRAINT "custom_name" TO "fruits_pkey";
ALTER TABLE "Fruits" ALTER COLUMN "name" SET DATA TYPE TEXT;

Prisma information

// Add your schema.prisma
// Add your code using Prisma Client

Environment & setup

  • OS: macOS,
  • Database: PostgreSQL
  • Node.js version: v20.13.1

Prisma Version

5.14.0
@omaziarz omaziarz added the kind/bug A reported bug. label May 29, 2024
@Jolg42 Jolg42 added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: migrate domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. topic: prisma migrate dev CLI: prisma migrate dev topic: postgresql topic: broken migrations bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels May 29, 2024
@Jolg42
Copy link
Contributor

Jolg42 commented May 29, 2024

I can confirm the problem

npx prisma migrate dev

Environment variables loaded from prisma/.env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "24331-dev" at "localhost:5432"

✔ Enter a name for the new migration: … 
Applying migration `20240529123216_`
Error: P3018

A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve

Migration name: 20240529123216_

Database error code: 42601

Database error:
ERROR: syntax error at or near ","

Position:
  0
  1 -- AlterTable
  2 ALTER TABLE "Fruits" RENAME CONSTRAINT "custom_name" TO "Fruits_pkey",

DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState(E42601), message: "syntax error at or near \",\"", detail: None, hint: None, position: Some(Original(84)), where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("scan.l"), line: Some(1127), routine: Some("scanner_yyerror") }

@janpio janpio changed the title Prisma Migrate generates incorrect sql when chaining different types of table altering operations Prisma Migrate generates incorrect sql when chaining different types of table altering operations (remove map from @id, add native type to other field) May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/bug A reported bug. topic: broken migrations topic: migrate topic: postgresql topic: prisma migrate dev CLI: prisma migrate dev
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants