sdispater / orator

The Orator ORM provides a simple yet beautiful ActiveRecord implementation.
https://orator-orm.com
MIT License
1.43k stars 173 forks source link

'Relation "migration" already exists' when running migrations (postgres) #412

Closed mehrenreich closed 1 year ago

mehrenreich commented 1 year ago

Moin,

I'm running migrations programmatically by using this snippet in a file "cli.py":

repository = DatabaseMigrationRepository(self.db, self.migrations_table)
migrator = Migrator(repository, self.db)

if not migrator.repository_exists():
    repository.create_repository()

migrator.set_connection("postgres")
migrator.run(self.migrations_path, pretend)

This was running fine until today when I was forced to create everything in a different schema than "public".

So I changed all table names by prepending the schema name "sih". I also needed to change the name of the "migrations" table to "sih.migrations". This seems to work, but only once. When calling it a 2nd time to execute another migration, I'm getting this error:

❯ python3 cli.py migrate ... --migrations-table=sih.migrations
DEBUG:orator.database_manager:Initiating connection postgres
DEBUG:orator.database_manager:Making connection for postgres
DEBUG:orator.database_manager:Preparing connection postgres
FEHLER:  Relation »migrations« existiert bereits
 (SQL: CREATE TABLE "sih"."migrations" ("migration" VARCHAR(255) NOT NULL, "batch" INTEGER NOT NULL) (None))Cannot execute migrations

It says "ERROR: Relation »migrations« already exists".

Before renaming everything it worked fine.

mehrenreich commented 1 year ago

OK, I was able to "solve" it by myself by setting the default search path:

ALTER USER myuser SET search_path TO sih;