From 1e3359aa48d2a4e338057b9fd4eb0b83cec42a6b Mon Sep 17 00:00:00 2001 From: byrman Date: Mon, 28 Feb 2022 11:58:25 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20Support=20mixins.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlmodel/main.py | 2 +- tests/test_mixin.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/test_mixin.py diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 4d6d2f2712..0118877afd 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -323,7 +323,7 @@ def __init__( # triggers an error base_is_table = False for base in bases: - config = getattr(base, "__config__") + config = getattr(base, "__config__", None) if config and getattr(config, "table", False): base_is_table = True break diff --git a/tests/test_mixin.py b/tests/test_mixin.py new file mode 100644 index 0000000000..1e81ac37bd --- /dev/null +++ b/tests/test_mixin.py @@ -0,0 +1,17 @@ +from typing import Optional + +import pytest +from sqlmodel import Field, SQLModel + + +class FooMixin: + pass + + +@pytest.mark.usefixtures("clear_sqlmodel") +def test_mixin(): + class Hero(FooMixin, SQLModel, table=True): + id: Optional[int] = Field(default=None, primary_key=True) + name: str + secret_name: str + age: Optional[int] = None From 168b9ae9d9385862a8d72e67346be92b9226a09a Mon Sep 17 00:00:00 2001 From: byrman Date: Mon, 28 Feb 2022 12:49:05 +0100 Subject: [PATCH 2/2] Add a docstring to the test. --- tests/test_mixin.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_mixin.py b/tests/test_mixin.py index 1e81ac37bd..48864e83ab 100644 --- a/tests/test_mixin.py +++ b/tests/test_mixin.py @@ -10,6 +10,12 @@ class FooMixin: @pytest.mark.usefixtures("clear_sqlmodel") def test_mixin(): + """Test SQLModel in combination with a mixin. + + https://github.com/tiangolo/sqlmodel/issues/254 + + """ + class Hero(FooMixin, SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str