From 3bc9135ca2f0a7f55e62f87938d1cdb7cc0a5af9 Mon Sep 17 00:00:00 2001 From: sinisaos Date: Thu, 8 Jan 2026 14:40:01 +0100 Subject: [PATCH 1/2] fix for issue #1301 --- piccolo/apps/migrations/auto/migration_manager.py | 9 +++++---- piccolo/query/constraints.py | 8 +++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/piccolo/apps/migrations/auto/migration_manager.py b/piccolo/apps/migrations/auto/migration_manager.py index 53f4a3a7c..ce085783f 100644 --- a/piccolo/apps/migrations/auto/migration_manager.py +++ b/piccolo/apps/migrations/auto/migration_manager.py @@ -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( diff --git a/piccolo/query/constraints.py b/piccolo/query/constraints.py index 7f6d1f565..4cbd383a5 100644 --- a/piccolo/query/constraints.py +++ b/piccolo/query/constraints.py @@ -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. """ @@ -40,6 +41,11 @@ async def get_fk_constraint_name(column: ForeignKey) -> str: column_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 + if not constraints: + return None return constraints[0]["fk_constraint_name"] From 0a039771cd4578737a31d8f546b2356ee9b9bf7d Mon Sep 17 00:00:00 2001 From: sinisaos Date: Sat, 10 Jan 2026 12:53:48 +0100 Subject: [PATCH 2/2] change return statement --- piccolo/query/constraints.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/piccolo/query/constraints.py b/piccolo/query/constraints.py index 4cbd383a5..a5859c100 100644 --- a/piccolo/query/constraints.py +++ b/piccolo/query/constraints.py @@ -44,9 +44,7 @@ async def get_fk_constraint_name(column: ForeignKey) -> Optional[str]: # 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 - if not constraints: - return None - return constraints[0]["fk_constraint_name"] + return constraints[0]["fk_constraint_name"] if constraints else None @dataclass