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 migration fails with postgres #15826
Comments
I think that is pretty much expected behavior.
(You can also of course keep using |
thank you for your comment, I actually tried to create my DB at first time with the also as I mentioned, I don't know why Prisma doesn't add |
Because that is exactly the job of |
So, if you created your DB with db push, you have to update your DB always using db push or your your data will get lost. |
You can also switch from |
I cleared my db and started my schema on a new db with the following command:
this command succeeded and created my db tables and needed schemas then I added a field to my schema as mentioned in the issue and run this command
this command failed and returned that error: 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: 20221020203914_update
Database error code: 42P07
Database error:
ERROR: relation "users" already exists
DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState(E42P07), message: "relation \"users\" already exists", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("heap.c"), line: Some(1200), routine: Some("heap_create_with_catalog") }
when I checked the created migration script I found it contains the whole sql commands for the whole schema not just the updated field here is the migration file content: -- CreateSchema
CREATE SCHEMA IF NOT EXISTS "account";
-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "server";
-- CreateTable
CREATE TABLE "account"."users" (
"id" TEXT NOT NULL,
"first_name" TEXT NOT NULL,
"last_name" TEXT NOT NULL,
"username" TEXT NOT NULL,
"email" TEXT NOT NULL,
"password" TEXT NOT NULL,
"role" TEXT NOT NULL,
"account_id" TEXT NOT NULL,
"verified" BOOLEAN DEFAULT false,
"active" BOOLEAN DEFAULT true,
"verificationCode" TEXT,
"created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "users_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "account"."accounts" (
"id" TEXT NOT NULL,
"zacId" TEXT NOT NULL,
"active" BOOLEAN DEFAULT false,
"created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "accounts_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "account"."payment_methods" (
"id" TEXT NOT NULL,
"primary" BOOLEAN DEFAULT true,
"account_id" TEXT NOT NULL,
"billing_address_id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "payment_methods_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "account"."billing_addresses" (
"id" TEXT NOT NULL,
"email" TEXT NOT NULL,
"address" TEXT NOT NULL,
"postal_code" TEXT NOT NULL,
"created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "billing_addresses_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "account"."plans" (
"id" TEXT NOT NULL,
"originalId" TEXT NOT NULL,
"price" DECIMAL(65,30) NOT NULL,
"duration" INTEGER NOT NULL DEFAULT 0,
"title" TEXT NOT NULL,
"account_id" TEXT NOT NULL,
"features" JSONB NOT NULL,
"subscribed_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
"created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "plans_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "server"."projects" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"myId" TEXT NOT NULL,
"account_id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "projects_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "account"."users"("email");
-- CreateIndex
CREATE INDEX "users_id_email_verified_verificationCode_idx" ON "account"."users"("id", "email", "verified", "verificationCode");
-- CreateIndex
CREATE UNIQUE INDEX "accounts_zacId_key" ON "account"."accounts"("zacId");
-- CreateIndex
CREATE UNIQUE INDEX "payment_methods_billing_address_id_key" ON "account"."payment_methods"("billing_address_id");
-- CreateIndex
CREATE UNIQUE INDEX "plans_account_id_key" ON "account"."plans"("account_id");
this is the reason that the migration fails you can do the same steps I mentioned and you will get the same issue thank you |
If you noticed, I am using I tried to remove the multiSchema option and added all models to the same schema and the migration is working fine, the problem is related to multi schema option, it regenerates the whole sql commands again |
Oh shoot, I indeed did not notice. From #1122 (comment):
So unfortunately for now this is expected in this early preview feature. |
Ok, so this issue does not happen anymore with 4.7.1 or main. |
Bug description
I have the following schema
I run the first command to push my schema to db as the following:
then I did an update on the following model:
then I run the following command:
it fails with the following error:
when I looked on the generated migration script I found that prisma regenerates the migration for the whole schema even the not changed tables!
that's why it outputs this error
Also I don't know why the generated migration script doesn't have CREATE IF NOT EXIST instead of CREATE
because this may fail also!
How to reproduce
Expected behavior
No response
Prisma information
Environment & setup
Prisma Version
The text was updated successfully, but these errors were encountered: