E
E
egorggegor2021-06-17 17:37:54
go
egorggegor, 2021-06-17 17:37:54

Why are migrations not running?

Hello!

I want to make the first migration for my database using migrate. But it gives the following error:
60cb5dad11980759136082.png

What could be the problem?

PS
Here is the running docker:
60cb5deb64833127492704.png

Here is the code when migrating up:

CREATE TABLE users
(
    id            serial       not null unique,
    name          varchar(255) not null,
    username      varchar(255) not null unique,
);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-06-17
@egorggegor

This usually means that a migration was previously attempted and an error occurred during the migration.
For example, you have only one migration (version 1) with the creation of the `users` table as in your case.
You run the up command, and there is an error in the migrations file.
In this case, the `users` table was not created in the database, but the `migrate` command has already created the schema_migrations table.
And not just created, but also wrote down there that your database corresponds to version 1 and set the `dirty` flag.

SELECT * FROM schema_migrations;
+---------+-------+
| version | dirty |
+---------+-------+
|     1   |   1   |
+---------+-------+
1 row in set (0.00 sec)

For good, you need to check which migrations you have completed and which have not.
Those. you need to understand which version now corresponds to what is in the database.
After understanding comes, you will need to specify the version that the base corresponds to.
migrate -path $PATH_TO_YOUR_MIGRATIONS -database $YOUR_DATABASE_URL force $VERSION

Where $VERSION is the version number that the base structure corresponds to.
If you have only one table and you have already fixed the error in the migration file, you can simply delete the `schema_migrations` table and stop reading :)
After executing the command with force $VERSION parameters, the "dirty" flag will be removed and you can call your command again `up` and it will succeed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question