Skip to content

Commit

Permalink
tests: MappedAsDataclass - skip table binding
Browse files Browse the repository at this point in the history
With SQLAlchemy 2.0.36 mapping as a dataclass while also providing
a ``__table__`` attribute is not supported.

Skip associated test when ``MappedAsDataclass`` is in use.

Fixes: pallets-eco#1378
  • Loading branch information
javacruft committed Feb 14, 2025
1 parent 3e3e92b commit 3810fe1
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions tests/test_model_bind.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import pytest
import sqlalchemy as sa
import sqlalchemy.orm as sa_orm

from flask_sqlalchemy import SQLAlchemy

Expand Down Expand Up @@ -76,15 +78,18 @@ class User(db.Model):


def test_explicit_table(db: SQLAlchemy) -> None:
user_table = db.Table(
"user",
sa.Column("id", sa.Integer, primary_key=True),
bind_key="auth",
)

class User(db.Model):
__bind_key__ = "other"
__table__ = user_table

assert User.__table__.metadata is db.metadatas["auth"]
assert "other" not in db.metadatas
if not issubclass(db.Model, (sa_orm.MappedAsDataclass)):
user_table = db.Table(
"user",
sa.Column("id", sa.Integer, primary_key=True),
bind_key="auth",
)

class User(db.Model):
__bind_key__ = "other"
__table__ = user_table

assert User.__table__.metadata is db.metadatas["auth"]
assert "other" not in db.metadatas
else:
pytest.skip("Explicit table binding with mapped dataclasses not supported")

0 comments on commit 3810fe1

Please sign in to comment.