File tree Expand file tree Collapse file tree 8 files changed +99
-3
lines changed Expand file tree Collapse file tree 8 files changed +99
-3
lines changed Original file line number Diff line number Diff line change @@ -18,3 +18,4 @@ Examples
18
18
all_of
19
19
inheritance
20
20
json
21
+ mixins
Original file line number Diff line number Diff line change
1
+ Mixin Classes
2
+ =============
3
+
4
+ SQLAlchemy supports mixin classes for models to add functionality. OpenAlchemy
5
+ supports this through the :samp: `x-mixins ` extension property that may define
6
+ one or more importable class to mix into the model class.
7
+
8
+ .. seealso ::
9
+
10
+ :ref: `mixins `
11
+ OpenAlchemy documentation for the :samp: `x-mixins ` value.
12
+
13
+ The following example adds the :samp: `sqlalchemy_mixins.TimestampsMixin ` to
14
+ the :samp: `Employee ` model:
15
+
16
+ .. literalinclude :: ../../../examples/mixins/example-spec.yml
17
+ :language: yaml
18
+ :linenos:
19
+
20
+ The following file uses OpenAlchemy to generate the SQLAlchemy models:
21
+
22
+ .. literalinclude :: ../../../examples/mixins/models.py
23
+ :language: python
24
+ :linenos:
25
+
26
+ The SQLAlchemy models generated by OpenAlchemy are equivalent to the following
27
+ traditional models file:
28
+
29
+ .. literalinclude :: ../../../examples/mixins/models_traditional.py
30
+ :language: python
31
+ :linenos:
32
+
33
+ OpenAlchemy will generate the following typed models:
34
+
35
+ .. literalinclude :: ../../../examples/mixins/models_auto.py
36
+ :language: python
37
+ :linenos:
Original file line number Diff line number Diff line change @@ -360,6 +360,8 @@ the documentation:
360
360
+------------------------------+----------------------------------------------------+
361
361
| :samp: `x-foreign-key-colum ` | :ref: `custom-foreign-key ` |
362
362
+------------------------------+----------------------------------------------------+
363
+ | :samp: `x-mixins ` | :ref: `mixins ` |
364
+ +------------------------------+----------------------------------------------------+
363
365
| :samp: `x-backrefs ` | :ref: `Models File Note <backrefs >` |
364
366
+------------------------------+----------------------------------------------------+
365
367
| :samp: `x-de-$ref ` | :ref: `from_dict Note <de-ref >` |
Original file line number Diff line number Diff line change @@ -12,3 +12,4 @@ Technical Details
12
12
relationships
13
13
inheritance
14
14
model
15
+ mixins
Original file line number Diff line number Diff line change @@ -120,9 +120,9 @@ The required :samp:`__mapper_args__` must be added using :samp:`x-kwargs`. For
120
120
example, for :samp: `Employee `:
121
121
122
122
.. code-block :: yaml
123
- :linenos :
123
+ :linenos :
124
124
125
- Employee :
125
+ Employee :
126
126
type : object
127
127
...
128
128
x-kwargs :
Original file line number Diff line number Diff line change
1
+ .. _mixins :
2
+
3
+ Mixin Classes
4
+ =============
5
+
6
+ SQLAlchemy supports adding common functionality from a class to many other
7
+ classes through mixin classes. OpenAlchemy supports this through the
8
+ :samp: `x-mixins ` extension property.
9
+
10
+ .. seealso ::
11
+
12
+ `SQLAlchemy mixin documentation <https://docs.sqlalchemy.org/en/13/orm/extensions/declarative/mixins.html#mixing-in-columns >`_
13
+ Documentation for SQLAlchemy mixin.
14
+
15
+ The following rules apply to the :samp: `x-mixins ` extension property:
16
+
17
+ * it can either be a string or a list of strings each of which defines one of
18
+ the mixin classes and
19
+ * each value must be the fully qualified dotted name of the class to use as the
20
+ mixin. Everything before the last dot will be used to import the module and
21
+ everything after will be used to retrieve the class from the module.
22
+
23
+ The following example adds the :samp: `sqlalchemy_mixins.TimestampsMixin ` to the
24
+ :samp: `Employee ` model:
25
+
26
+ .. code-block :: yaml
27
+ :linenos :
28
+
29
+ Employee :
30
+ type : object
31
+ x-mixins : sqlalchemy_mixins.TimestampsMixin
32
+ ...
33
+
34
+ Alternatively, multiple mixins can be defined using a list:
35
+
36
+ .. code-block :: yaml
37
+ :linenos :
38
+
39
+ Employee :
40
+ type : object
41
+ x-mixins :
42
+ - sqlalchemy_mixins.TimestampsMixin
43
+ - sqlalchemy_mixins.EagerLoadMixin
44
+ ...
45
+
46
+ .. seealso ::
47
+
48
+ `sqlalchemy_mixins package <https://github.com/absent1706/sqlalchemy-mixins >`_
49
+ Package that defines some helpful SQLAlchemy mixins.
Original file line number Diff line number Diff line change @@ -9,6 +9,8 @@ class Employee(Base, TimestampsMixin):
9
9
"""Person that works for a company."""
10
10
11
11
__tablename__ = "employee"
12
+ __abstract__ = False
13
+
12
14
id = sa .Column (sa .Integer , primary_key = True , autoincrement = True )
13
15
name = sa .Column (sa .String , index = True )
14
16
division = sa .Column (sa .String , index = True )
Original file line number Diff line number Diff line change @@ -562,7 +562,10 @@ def test_mixin(monkeypatch):
562
562
mixin_class = type (
563
563
"Mixin1" ,
564
564
(),
565
- {"property_2" : sqlalchemy .column .Column (sqlalchemy .column .Integer )},
565
+ {
566
+ "property_2" : sqlalchemy .column .Column (sqlalchemy .column .Integer ),
567
+ "__abstract__" : True ,
568
+ },
566
569
)
567
570
mock_import_module .return_value .Mixin1 = mixin_class
568
571
monkeypatch .setattr (importlib , "import_module" , mock_import_module )
@@ -581,6 +584,7 @@ def test_mixin(monkeypatch):
581
584
)
582
585
583
586
assert hasattr (model , "property_2" )
587
+ assert model .__abstract__ is False
584
588
585
589
586
590
class TestGetSchema :
You can’t perform that action at this time.
0 commit comments