prisma: `prisma migrate resolve --applied` not working on new project, `migration ... could not be found.` when using PowerShell on Windows

[Edit by @janpio] Most recent update, see this comment: https://github.com/prisma/prisma/issues/17558#issuecomment-2037194817


As mentioned in another issue: Following the Tutorial to baseline a database when using prisma in a new project with an existing database leads to an error:

The final step npx prisma migrate resolve --applied 0_init leads to the following error:

Error: P3017

The migration 0_init could not be found. Please make sure that the migration exists, and that you included the whole name of >the directory. (example: “20201207184859_initial_migration”)

The database (in my case) is mssql - if it makes any difference. The steps before (pulling, generating, creating migrations-directory and creating diff-sql) are all successful.

I tried the following things:

  • Putting folder in quotes: npx prisma migrate resolve --applied "0_init"
  • Passing the complete path: npx prisma migrate resolve --applied ./prisma/migrations/0_init
  • Passing the complete path with filename: npx prisma migrate resolve --applied ./prisma/migrations/0_init/migration.sql
  • Changing into prisma-directory and executing command
  • Changing into prisma/migrations-directory and executing command
  • Renaming folder “0_init” to “20201207184859_initial_migration” (I know, it’s stupid) and running npx prisma migrate resolve --applied 20201207184859_initial_migration

… to no avail.


For reference: Here is the relevant comment from the other issue:

Hi I'm facing the same issue as @mrunalhajare. Here is what I'm doing:

1. `npx prisma db pull` -- Populates schema.prisma correctly
2. `npx prisma generate` -- Success generating
3. `mkdir -p prisma/migrations/init`
4. `npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > prisma/migrations/init/migration.sql` -- Creates migration.sql file in prisma/migrations/init directory
5. `npx prisma migrate resolve --applied init` -- ERROR

> Error: P3017
> 
> The migration init could not be found. Please make sure that the migration exists, and that you included the whole name of the directory. (example: "20201207184859_initial_migration")

NOTE: I did manually create `migration_lock.toml` before step 5 above, but I get the same error.

Anything I'm doing wrong?

_Originally posted by @simplypress in https://github.com/prisma/prisma/issues/11555#issuecomment-1331671279_

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 6
  • Comments: 29 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Based on the excellent information that @notmyfiles provided, here is a reproduction repository that shows both a working and a non-working migration file: https://github.com/janpio/prisma-repro-17558 The cause seems to be when you use migrate diff via Powershell, which is the default in VS Code and I also assume IntelliJ.

Maybe follow the steps in the README @notmyfiles to confirm. (Also for @kraighamady and @rahInoue - but you using VSCode and Intellij would fit nicely here of course)

We definitely need to fix migrate resolve here (and other commands that probably would have a problem with this file).

Workaround

Based on the analysis it might be enough to change the codepage or encoding of the file from UTF-16 with BOM to UTF-8, with or without BOM. Notepad++ for example can do that for you and many other tools. Alternatively you can of course recreate the file with another terminal. Cmd worked fine for me.

This was fixed by changing the encoding of the generated migration.sql file! I changed it to UTF-8 and it works like a charm!

I checked about the first 2 commands: I wrote it down in the wrong order, but did them in the correct order 🙃. Anyway, I tried everything again - once in the VSCode terminal (which uses powershell) and once outside VSCode using cmd.exe. The command npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > prisma/migrations/0_init/migration.sql seems to be the culprit – VSCode/powershell lead to the error, cmd not. As you noticed: VSCode or powershell seem to use a different encoding. So either prisma migrate resolve or … diff has to be fixed to deal with different encodings. Thanks for your detective work!

@janpio I just deleted my migration.sql file and reran the last two commands from the quickstart and it worked. The key was I used Git Bash instead of PS or the default PS in VSCode.

I didn’t take a look at the encoding of the files, but just recreating the initial migration in bash did the trick.

you can also change the encoding format in vs code and run the command from your usual terminal, like mentioned below

If you’re using VsCode and your 0_init/migration.sql is in the wrong encoding you can:

  • Open the file 0_init/migration.sql
  • Press Ctrl+Shift+p
  • Type “Change File Encoding”
  • Select “Save With Encoding”
  • Select “UTF-8”

got it from here https://github.com/prisma/prisma/issues/14762#issuecomment-1533177502

I could isolate the difference of your and my project, it is literally only the encoding of the file: image

Somehow that is throwing us off. But I have a reproduction now thanks to your repository @notmyfiles - so we can investigate this. Thanks!

Ahh, creating the diff via VSCode is a good idea. Let me do that. I used cmd externally via Windows Terminal.

Update: Yep, that’s it.

We just released version 5.12.1 which includes a new option --output for migrate diff. It takes a file path, where the result of your migrate diff command output is written - so no more need to do > foo.sql in PowerShell which was the source of this problem!

The old command still works of course:

npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > prisma/migrations/0_init/migration.sql

But instead you can - and should if you are using PowerShell! - now also use this:

npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script --output prisma/migrations/0_init/migration.sql

Let me know if this solves the problem as well for you, as did changing the file encoding manually.

Hi, thank you for al the context, but I’m still having the same issue … i’m in MacOs with VsCode

Any idea guys would be helpful 😃

thank you so much

Check the location of your schema.prisma file, seems like the migration folder should be relative to that file as follows:

Correct options:

./prisma/schema.prisma
./prisma/migrations/0_init/migration.sql
./schema.prisma
./migrations/0_init/migration.sql

It will fail if you mix the above locations. E.g.:

./schema.prisma
./prisma/migrations/0_init/migration.sql

That solved it for me, good luck!

I could isolate the difference of your and my project, it is literally only the encoding of the file: image

Somehow that is throwing us off. But I have a reproduction now thanks to your repository @notmyfiles - so we can investigate this. Thanks!

I could isolate the difference of your and my project, it is literally only the encoding of the file: image

Somehow that is throwing us off. But I have a reproduction now thanks to your repository @notmyfiles - so we can investigate this. Thanks!

I can confirm it’s the file encoding as well.

After changing it to “Windows 1252”, the issue goes away.

I am experiencing the same issue, but it only occurs with IntelliJ IDEA. I followed these steps:

  1. nest new check-prisma
  2. cd check-prisma
  3. npm install prisma --save-dev
  4. npx prisma init
  5. npx prisma db pull
  6. mkdir -p prisma/migrations/0_init
  7. npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > prisma/migrations/0_init/migration.sql
  8. npx prisma migrate resolve --applied 0_init

I only executed these commands above. In Visual Studio Code, applying “0_init” was successful, but in IntelliJ IDEA, it failed. I tried these steps on a Windows PC and PowerShell.

This was fixed by changing the encoding of the generated migration.sql file! I changed it to UTF-8 and it works like a charm!

Worked for me, thanks!!

@janpio I just deleted my migration.sql file and reran the last two commands from the quickstart and it worked. The key was I used Git Bash instead of PS or the default PS in VSCode.

I didn’t take a look at the encoding of the files, but just recreating the initial migration in bash did the trick.

This worked for me, thanks!

I faced similar issue when using powershell. Try the command from git bash it works. npx prisma migrate resolve --applied 0_init

Yes, it only due to the encoding issue.

Thank you for investigating! I have tried the prisma db pull in various databases, but some encodings have caused character corruption in IntelliJ. This may be the same cause. If it continues, I will report back again. Thank you!