Skip to content

Commit 75c2ca4

Browse files
author
David Andersson
committed
Update docs
1 parent b17d21f commit 75c2ca4

File tree

6 files changed

+98
-1
lines changed

6 files changed

+98
-1
lines changed

docs/source/examples/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Examples
99
alembic
1010
simple
1111
default
12+
server_default
1213
read_only
1314
write_only
1415
relationship/index
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Server Default
2+
==============
3+
4+
OpenAlchemy supports defining a default value generated by the database
5+
through the :samp:`x-server-default` extension property. It is similar to the
6+
OpenAPI :samp:`default` property except that the default value is calculated
7+
by the database rather than the application.
8+
9+
.. seealso::
10+
11+
:ref:`server-default`
12+
OpenAlchemy documentation for the server default value.
13+
14+
:ref:`default`
15+
OpenAlchemy documentation for the default value.
16+
17+
`SQLAlchemy Server Default <https://docs.sqlalchemy.org/en/13/core/metadata.html#sqlalchemy.schema.Column.params.server_default>`_
18+
Documentation for the SQLAlchemy server default.
19+
20+
The following example defines a default value for the :samp:`name` property of
21+
:samp:`Employee`:
22+
23+
.. literalinclude:: ../../../examples/server_default/example-spec.yml
24+
:language: yaml
25+
:linenos:
26+
27+
The following file uses OpenAlchemy to generate the SQLAlchemy models:
28+
29+
.. literalinclude:: ../../../examples/server_default/models.py
30+
:language: python
31+
:linenos:
32+
33+
The SQLAlchemy models generated by OpenAlchemy are equivalent to the following
34+
traditional models file:
35+
36+
.. literalinclude:: ../../../examples/server_default/models_traditional.py
37+
:language: python
38+
:linenos:
39+
40+
OpenAlchemy will generate the following typed models:
41+
42+
.. literalinclude:: ../../../examples/server_default/models_auto.py
43+
:language: python
44+
:linenos:

docs/source/technical_details/column_modifiers.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,45 @@ OpenAlchemy.
457457
`SQLAlchemy "Scalar Default" <https://docs.sqlalchemy.org/en/13/core/defaults.html#scalar-defaults>`_
458458
Documentation for the scalar default value in SQLAlchemy.
459459

460+
.. _server-default:
461+
462+
Server Default
463+
--------------
464+
465+
To add a default value for a column to be generated by the database, use the
466+
:samp:`x-server-default` extension property:
467+
468+
.. code-block:: yaml
469+
:linenos:
470+
471+
Employee:
472+
type: object
473+
x-tablename: employee
474+
properties:
475+
id:
476+
type: integer
477+
name:
478+
type: string
479+
x-server-default: Unknown
480+
481+
The default value is added to the column constructor using the "Server
482+
Default" in SQLAlchemy. The following property types support a server default
483+
value (including all their formats supported by OpenAlchemy):
484+
485+
* :samp:`integer`,
486+
* :samp:`number`,
487+
* :samp:`string` and
488+
* :samp:`boolean`.
489+
490+
Adding a server default to a :samp:`object` or :samp:`array` type is not valid
491+
in OpenAlchemy. Server default is also not supported by any property that sets
492+
:samp:`x-json` to :samp:`true`.
493+
494+
.. seealso::
495+
496+
`SQLAlchemy Server Default <https://docs.sqlalchemy.org/en/13/core/metadata.html#sqlalchemy.schema.Column.params.server_default>`_
497+
Documentation for the SQLAlchemy server default.
498+
460499
.. _read-only:
461500

462501
readOnly

examples/server_default/example-spec.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ components:
3535
type: string
3636
description: The name of the employee.
3737
example: David Andersson
38-
x-server-default: "Unknown"
38+
x-server-default: Unknown

open_alchemy/schemas/validation/property_/json.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ def check(schemas: oa_types.Schemas, schema: oa_types.Schema) -> types.Result:
4646
)
4747
if autoincrement is not None:
4848
return types.Result(False, "json properties do not support x-autoincrement")
49+
server_default = helpers.peek.peek_key(
50+
schema=schema, schemas=schemas, key="x-server-default"
51+
)
52+
if server_default is not None:
53+
return types.Result(
54+
False, "json properties do not support x-server-default"
55+
)
4956
# Checks kwargs, foreign key and foreign key kwargs
5057
kwargs_result = simple.check_kwargs(schema=schema, schemas=schemas)
5158
if kwargs_result is not None:

tests/open_alchemy/schemas/validation/property_/test_json.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@
158158
(False, "json properties do not support x-autoincrement"),
159159
id="x-autoincrement defined",
160160
),
161+
pytest.param(
162+
{"x-server-default": "False"},
163+
{},
164+
(False, "json properties do not support x-server-default"),
165+
id="x-server-default defined",
166+
),
161167
pytest.param(
162168
{"x-foreign-key": True},
163169
{},

0 commit comments

Comments
 (0)