Fix TypeError when sorting mixed compound/single foreign keys

sorted() on a foreign_keys list containing both compound (column=None)
and single-column ForeignKeys raised TypeError because dataclass
ordering compared None against str. column/other_column are now
excluded from comparison - ordering and equality use the always
populated columns/other_columns lists instead.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Simon Willison 2026-07-05 14:26:56 -07:00
commit d5bf51df35
2 changed files with 32 additions and 2 deletions

View file

@ -182,9 +182,11 @@ class ForeignKey:
"""
table: str
column: Optional[str]
# column/other_column are None for compound keys, which would break
# ordering against str values - comparison uses columns/other_columns
column: Optional[str] = field(compare=False)
other_table: str
other_column: Optional[str]
other_column: Optional[str] = field(compare=False)
columns: List[str] = field(default_factory=list)
other_columns: List[str] = field(default_factory=list)
is_compound: bool = False