sdispater / orator

The Orator ORM provides a simple yet beautiful ActiveRecord implementation.

Home Page:https://orator-orm.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

mehrenreich opened this issue · comments

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.

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

ALTER USER myuser SET search_path TO sih;