Skip to content

Cannot drop index needed in a foreign key constraint #530

Description

@Lumabots
Image

After doing this change, the migration works:

from tortoise import BaseDBAsyncClient

RUN_IN_TRANSACTION = True


async def upgrade(db: BaseDBAsyncClient) -> str:
    return """
        ALTER TABLE `tagdata` DROP INDEX `uid_tagdata_guild_i_6441d7`;
        ALTER TABLE `tagdata` ADD UNIQUE INDEX `uid_tagdata_guild_i_41971f` (`guild_id`, `name`);"""


async def downgrade(db: BaseDBAsyncClient) -> str:
    return """
        ALTER TABLE `tagdata` DROP INDEX `uid_tagdata_guild_i_41971f`;
        ALTER TABLE `tagdata` ADD UNIQUE INDEX `uid_tagdata_guild_i_6441d7` (`guild_id`, `user_id`);"""

however when trying to to upgrade it it raise:

  File "/Users/luma/Code/Lumabot-Alpha/.venv/lib/python3.11/site-packages/aerich/cli.py", line 149, in upgrade
    migrated = await command.upgrade(run_in_transaction=in_transaction, fake=fake)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/luma/Code/Lumabot-Alpha/.venv/lib/python3.11/site-packages/aerich/__init__.py", line 137, in upgrade
    await self._upgrade(conn, version_file, fake, version_module)
  File "/Users/luma/Code/Lumabot-Alpha/.venv/lib/python3.11/site-packages/aerich/__init__.py", line 115, in _upgrade
    await conn.execute_script(await upgrade(conn))
  File "/Users/luma/Code/Lumabot-Alpha/.venv/lib/python3.11/site-packages/tortoise/backends/mysql/client.py", line 58, in translate_exceptions_
    raise OperationalError(exc)
tortoise.exceptions.OperationalError: (1553, "Cannot drop index 'uid_tagdata_guild_i_6441d7': needed in a foreign key constraint")

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions