How to Resolve VersionMismatch(1) Error with sqlx::migrate!

I have a Rust project with the following structure:

project_root/
├── src/
│   ├── main.rs
│   └── other_files.rs
├── migrations/
│   ├── 00001_create_users_table.sql
│   ├── 00002_add_index_to_users.sql
│   └── other_migrations.sql
└── Cargo.toml

The migrations directory contains my migration files. I am using the sqlx::migrate! macro to run these migrations with the following code:
sqlx::migrate!("./migrations").run(&connection).await.expect("Error running DB migrations");

However, this code results in a panic with the following error message:
Error running DB migrations: VersionMismatch(1)

The migrations seemed to run correctly the first time I executed the cargo run command. I need help resolving this VersionMismatch(1) error when using the migrate! macro.

Have you changed any of the migrations files? If the checksum of a migration file changes, it results in the error you are seeing (here the source). What is weird is that the version of the failing migration is 1, even though there is no migration with that version in your ./migrations directory. Unless other_migrations.sql is just a placeholder (it doesn't match the expected file naming convention of sqlx, so it would be silently ignored if it wasn't).

1 Like

Yes, The first time I executed the cargo run command, I verified that the table was created successfully. Later, I added some ALTER SQL commands to the 00001_create_users_table.sql file and re-ran the cargo run command, which led to the error. I believe the issue might be related to the migration files being modified after they were initially applied to the database.

I updated the help request with the actual name I am using in my project. I do have file name that starts with 0001 in ./migrations directory.

Do I need to delete something from the database to solve this problem?
Or can I solve it from the code? I want to execute the sql every time I do cargo run.

You can delete the _sqlx_migrations table from your database.

That is not a problem. A problem is when you apply a migration and change the migration file afterwards.

1 Like

Thank you so much this worked! :blush:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.