1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
.. meta::
:description:
SQLALchemy integration with the marshmallow (de)serialization library.
**********************
marshmallow-sqlalchemy
**********************
`SQLAlchemy <http://www.sqlalchemy.org/>`_ integration with the `marshmallow <https://marshmallow.readthedocs.io/en/latest/>`_ (de)serialization library.
Release v\ |version| (:ref:`Changelog <changelog>`)
----
Declare your models
===================
.. tab-set::
.. tab-item:: SQLAlchemy 1.4
:sync: sqla1
.. code-block:: python
import sqlalchemy as sa
from sqlalchemy.orm import (
DeclarativeBase,
backref,
relationship,
sessionmaker,
)
from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field
engine = sa.create_engine("sqlite:///:memory:")
Session = sessionmaker(engine)
class Base(DeclarativeBase):
pass
class Author(Base):
__tablename__ = "authors"
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String, nullable=False)
def __repr__(self):
return f"<Author(name={self.name!r})>"
class Book(Base):
__tablename__ = "books"
id = sa.Column(sa.Integer, primary_key=True)
title = sa.Column(sa.String)
author_id = sa.Column(sa.Integer, sa.ForeignKey("authors.id"))
author = relationship("Author", backref=backref("books"))
.. tab-item:: SQLAlchemy 2
:sync: sqla2
.. code-block:: python
import sqlalchemy as sa
from sqlalchemy.orm import (
DeclarativeBase,
backref,
relationship,
sessionmaker,
mapped_column,
Mapped,
)
from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field
engine = sa.create_engine("sqlite:///:memory:")
Session = sessionmaker(engine)
class Base(DeclarativeBase):
pass
class Author(Base):
__tablename__ = "authors"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(nullable=False)
def __repr__(self):
return f"<Author(name={self.name!r})>"
class Book(Base):
__tablename__ = "books"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column()
author_id: Mapped[int] = mapped_column(sa.ForeignKey("authors.id"))
author: Mapped["Author"] = relationship("Author", backref=backref("books"))
.. include:: ../README.rst
:start-after: .. start elevator-pitch
:end-before: .. end elevator-pitch
.. toctree::
:maxdepth: 1
:hidden:
:titlesonly:
Home <self>
Learn
=====
.. toctree::
:caption: Learn
:maxdepth: 2
recipes
API reference
=============
.. toctree::
:caption: API reference
:maxdepth: 1
api_reference
Project info
============
.. toctree::
:caption: Project info
:maxdepth: 1
changelog
contributing
authors
license
.. toctree::
:hidden:
:caption: Useful links
marshmallow-sqlalchemy @ PyPI <https://pypi.org/project/marshmallow-sqlalchemy/>
marshmallow-sqlalchemy @ GitHub <https://github.com/marshmallow-code/marshmallow-sqlalchemy/>
Issue Tracker <https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues>
|