Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions piccolo/apps/migrations/auto/migration_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,12 @@ async def _run_alter_columns(self, backwards: bool = False):
constraint_name = await get_fk_constraint_name(
column=fk_column
)
await self._run_query(
_Table.alter().drop_constraint(
constraint_name=constraint_name
if constraint_name:
await self._run_query(
_Table.alter().drop_constraint(
constraint_name=constraint_name
)
)
)

# Then add a new foreign key constraint
await self._run_query(
Expand Down
8 changes: 6 additions & 2 deletions piccolo/query/constraints.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from dataclasses import dataclass
from typing import Optional

from piccolo.columns import ForeignKey
from piccolo.columns.base import OnDelete, OnUpdate


async def get_fk_constraint_name(column: ForeignKey) -> str:
async def get_fk_constraint_name(column: ForeignKey) -> Optional[str]:
"""
Checks what the foreign key constraint is called in the database.
"""
Expand Down Expand Up @@ -40,7 +41,10 @@ async def get_fk_constraint_name(column: ForeignKey) -> str:
column_name,
)

return constraints[0]["fk_constraint_name"]
# if we change the column type from a non-FK column to
# an FK column, the previous column type has no FK constraints
# and we skip this to allow the migration to continue
return constraints[0]["fk_constraint_name"] if constraints else None


@dataclass
Expand Down