Skip to content

Commit

Permalink
Provide query_class via backref for legacy use
Browse files Browse the repository at this point in the history
  • Loading branch information
jace committed May 29, 2023
1 parent 04ee3f5 commit 8b2750d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ required_plugins = [
minversion = '6.0'
addopts = '--doctest-modules --ignore setup.py --cov-report=term-missing'
doctest_optionflags = ['ALLOW_UNICODE', 'ALLOW_BYTES']
filterwarnings = ['ignore::DeprecationWarning', 'ignore::UserWarning']
env = ['FLASK_ENV=testing']
remote_data_strict = true

Expand Down
4 changes: 4 additions & 0 deletions src/coaster/sqlalchemy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Other(Model):
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Query as QueryBase
from sqlalchemy.orm import backref as backref_base
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship as relationship_base
from sqlalchemy.orm.dynamic import AppenderMixin
Expand All @@ -88,9 +89,11 @@ class Other(Model):
'timestamp_now',
'jsonb',
'Query',
'QueryProperty',
'ModelBase',
'DeclarativeBase',
'relationship',
'backref',
]


Expand Down Expand Up @@ -377,3 +380,4 @@ def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _T:
#: Wrap :func:`~sqlalchemy.orm.relationship` to insert a default query_class compatible
#: with Flask-SQLAlchemy's
relationship = _create_relationship_wrapper(relationship_base)
backref = _create_relationship_wrapper(backref_base)
27 changes: 27 additions & 0 deletions tests/coaster_tests/sqlalchemy_modelbase_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ModelBase,
ModelWarning,
Query,
backref,
int_pkey,
relationship,
)
Expand Down Expand Up @@ -339,6 +340,32 @@ class RelatedModel(Model): # skipcq: PTC-W0065
assert isinstance(TestModel().related, Query)


def test_backref_query_class() -> None:
"""Relationships via backref get Coaster's Query."""

class Model(ModelBase, DeclarativeBase):
"""Test Model base."""

class TestModel(Model):
"""Test model."""

__tablename__ = 'test_model'
pkey: Mapped[int_pkey]

class RelatedModel(Model): # pylint: disable=unused-variable # skipcq: PTC-W0065
"""Related model."""

__tablename__ = 'related_model'
pkey: Mapped[int_pkey]
test_id: Mapped[int] = mapped_column(sa.ForeignKey('test_model.pkey'))
with pytest.warns(ModelWarning):
test: Mapped[TestModel] = relationship(
backref=backref('related', lazy='dynamic')
)

assert isinstance(TestModel().related, Query) # type: ignore[attr-defined]


def test_backref_warning() -> None:
"""Relationships using a backref raise a warning."""

Expand Down

0 comments on commit 8b2750d

Please sign in to comment.