tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
820 stars 94 forks source link

PostgreSQL does not allow dropping unique indexes via DROP INDEX #180

Open PythonCoderAS opened 3 years ago

PythonCoderAS commented 3 years ago

If a migration containing a "DROP INDEX" command with a unique index is executed on PostgreSQL, an error is raised, claiming

asyncpg.exceptions.DependentObjectsStillExistError: cannot drop index uid_imagetempla_guild_i_25a69e because constraint uid_imagetempla_guild_i_25a69e on table imagetemplate requires it
HINT:  You can drop constraint uid_imagetempla_guild_i_25a69e on table imagetemplate instead.
PythonCoderAS commented 1 year ago

I had to modify the original SQL to this:

async def upgrade(db: BaseDBAsyncClient) -> str:
    return """
        CREATE UNIQUE INDEX "uid_statistic_channel_f53e9b" ON "statistic" ("channel_id", "thread_id", "author_id", "month");
        ALTER TABLE "statistic" DROP CONSTRAINT "uid_statistic_channel_3743df";"""

async def downgrade(db: BaseDBAsyncClient) -> str:
    return """
        ALTER TABLE "statistic" ADD CONSTRAINT "uid_statistic_channel_3743df" UNIQUE ("channel_id", "thread_id", "author_id");
        DROP INDEX "uid_statistic_channel_f53e9b";"""