aerich icon indicating copy to clipboard operation
aerich copied to clipboard

PostgreSQL does not allow dropping unique indexes via DROP INDEX

Open PythonCoderAS opened this issue 4 years ago • 1 comments

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 avatar Jul 24 '21 22:07 PythonCoderAS

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";"""

PythonCoderAS avatar May 01 '23 16:05 PythonCoderAS