Skip to content

Commit bc18cb5

Browse files
authored
fix: BigIntPrimaryKey does not respect schema names (#469)
BigIntPrimaryKey will now respect schema names. Fixes #466
1 parent 33e7d2f commit bc18cb5

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

advanced_alchemy/mixins/bigint.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
1+
from typing import Any, Optional
2+
13
from sqlalchemy import Sequence
24
from sqlalchemy.orm import Mapped, declarative_mixin, declared_attr, mapped_column
35

46
from advanced_alchemy.types import BigIntIdentity
57

68

9+
def _get_schema(cls: "BigIntPrimaryKey") -> Optional[str]: # pragma: nocover
10+
"""Get the schema for the class if set via __table_args__, __table__, or __table_kwargs__."""
11+
table_args = getattr(cls, "__table_args__", None)
12+
if isinstance(table_args, dict) and "schema" in table_args:
13+
return table_args["schema"] # type: ignore
14+
if isinstance(table_args, tuple) and table_args and isinstance(table_args[-1], dict) and "schema" in table_args[-1]:
15+
return table_args[-1]["schema"] # type: ignore
16+
if hasattr(cls, "__table__") and hasattr(cls.__table__, "schema"): # pyright: ignore
17+
return cls.__table__.schema # type: ignore[no-any-return]
18+
table_kwargs = getattr(cls, "__table_kwargs__", None)
19+
if isinstance(table_kwargs, dict) and "schema" in table_kwargs:
20+
return table_kwargs["schema"] # type: ignore
21+
return None
22+
23+
724
@declarative_mixin
825
class BigIntPrimaryKey:
926
"""BigInt Primary Key Field Mixin."""
1027

1128
@declared_attr
1229
def id(cls) -> Mapped[int]:
1330
"""BigInt Primary key column."""
31+
seq_kwargs: dict[str, Any] = {"optional": False}
32+
if schema := _get_schema(cls):
33+
seq_kwargs["schema"] = schema
1434
return mapped_column(
1535
BigIntIdentity,
16-
Sequence(f"{cls.__tablename__}_id_seq", optional=False), # type: ignore[attr-defined]
36+
Sequence(f"{cls.__tablename__}_id_seq", **seq_kwargs), # type: ignore[attr-defined]
1737
primary_key=True,
1838
)

sonar-project.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ sonar.coverage.exclusions=\
2323
advanced_alchemy/service/pagination.py, \
2424
advanced_alchemy/extensions/litestar/plugins/init/config/*.py, \
2525
advanced_alchemy/extensions/sanic/config.py, \
26-
advanced_alchemy/extensions/sanic/extension.py
26+
advanced_alchemy/extensions/sanic/extension.py \
27+
advanced_alchemy/mixins/bigint.py
2728
sonar.cpd.exclusions=\
2829
advanced_alchemy/repository/memory/_sync.py, \
2930
advanced_alchemy/repository/memory/_async.py, \

0 commit comments

Comments
 (0)