Skip to content

Commit 748d802

Browse files
authored
Fix for issue #1301 (#1321)
* fix for issue #1301 * change return statement
1 parent c027431 commit 748d802

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

piccolo/apps/migrations/auto/migration_manager.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,12 @@ async def _run_alter_columns(self, backwards: bool = False):
546546
constraint_name = await get_fk_constraint_name(
547547
column=fk_column
548548
)
549-
await self._run_query(
550-
_Table.alter().drop_constraint(
551-
constraint_name=constraint_name
549+
if constraint_name:
550+
await self._run_query(
551+
_Table.alter().drop_constraint(
552+
constraint_name=constraint_name
553+
)
552554
)
553-
)
554555

555556
# Then add a new foreign key constraint
556557
await self._run_query(

piccolo/query/constraints.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from dataclasses import dataclass
2+
from typing import Optional
23

34
from piccolo.columns import ForeignKey
45
from piccolo.columns.base import OnDelete, OnUpdate
56

67

7-
async def get_fk_constraint_name(column: ForeignKey) -> str:
8+
async def get_fk_constraint_name(column: ForeignKey) -> Optional[str]:
89
"""
910
Checks what the foreign key constraint is called in the database.
1011
"""
@@ -40,7 +41,10 @@ async def get_fk_constraint_name(column: ForeignKey) -> str:
4041
column_name,
4142
)
4243

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

4549

4650
@dataclass

0 commit comments

Comments
 (0)