Skip to content

Commit e94a52a

Browse files
authored
fix: add warning message when using fallback value for a primary key (#529)
Add warning to indicate that the `nanoid` / `uuid-utils` libraries are missing and behavior is falling back to a built-in.
1 parent cf3ddfc commit e94a52a

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

advanced_alchemy/mixins/nanoid.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import TYPE_CHECKING
1+
import logging
2+
from typing import TYPE_CHECKING, Any
23

34
from sqlalchemy.orm import Mapped, declarative_mixin, mapped_column
45

@@ -12,10 +13,17 @@
1213
else:
1314
from uuid import uuid4 as nanoid # type: ignore[assignment,unused-ignore]
1415

16+
logger = logging.getLogger("advanced_alchemy")
17+
1518

1619
@declarative_mixin
1720
class NanoIDPrimaryKey(SentinelMixin):
1821
"""Nano ID Primary Key Field Mixin."""
1922

23+
def __init_subclass__(cls, **kwargs: Any) -> None:
24+
super().__init_subclass__(**kwargs)
25+
if not NANOID_INSTALLED and not cls.__module__.startswith("advanced_alchemy"): # pragma: no cover
26+
logger.warning("`fastnanoid` not installed, falling back to `uuid4` for NanoID generation.")
27+
2028
id: Mapped[str] = mapped_column(default=nanoid, primary_key=True)
2129
"""Nano ID Primary key column."""

advanced_alchemy/mixins/uuid.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import TYPE_CHECKING
1+
import logging
2+
from typing import TYPE_CHECKING, Any
23
from uuid import UUID, uuid4
34

45
from sqlalchemy.orm import Mapped, declarative_mixin, mapped_column
@@ -18,6 +19,8 @@
1819
uuid6 = uuid4 # type: ignore[assignment, unused-ignore]
1920
uuid7 = uuid4 # type: ignore[assignment, unused-ignore]
2021

22+
logger = logging.getLogger("advanced_alchemy")
23+
2124

2225
@declarative_mixin
2326
class UUIDPrimaryKey(SentinelMixin):
@@ -31,6 +34,11 @@ class UUIDPrimaryKey(SentinelMixin):
3134
class UUIDv6PrimaryKey(SentinelMixin):
3235
"""UUID v6 Primary Key Field Mixin."""
3336

37+
def __init_subclass__(cls, **kwargs: Any) -> None:
38+
super().__init_subclass__(**kwargs)
39+
if not UUID_UTILS_INSTALLED and not cls.__module__.startswith("advanced_alchemy"): # pragma: no cover
40+
logger.warning("`uuid-utils` not installed, falling back to `uuid4` for UUID v6 generation.")
41+
3442
id: Mapped[UUID] = mapped_column(default=uuid6, primary_key=True)
3543
"""UUID Primary key column."""
3644

@@ -39,5 +47,10 @@ class UUIDv6PrimaryKey(SentinelMixin):
3947
class UUIDv7PrimaryKey(SentinelMixin):
4048
"""UUID v7 Primary Key Field Mixin."""
4149

50+
def __init_subclass__(cls, **kwargs: Any) -> None:
51+
super().__init_subclass__(**kwargs)
52+
if not UUID_UTILS_INSTALLED and not cls.__module__.startswith("advanced_alchemy"): # pragma: no cover
53+
logger.warning("`uuid-utils` not installed, falling back to `uuid4` for UUID v7 generation.")
54+
4255
id: Mapped[UUID] = mapped_column(default=uuid7, primary_key=True)
4356
"""UUID Primary key column."""

0 commit comments

Comments
 (0)