
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/test_orm_mapped_v2.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_test_orm_mapped_v2.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_test_orm_mapped_v2.py:

New ORM Declarative Mapping Style
=================================

``SQLAlchemy>=2`` introduced a new way to construct mappings using the
``sqlalchemy.orm.DeclarativeBase`` base class.
This example shows how to use GeoAlchemy2 types in this context.

.. GENERATED FROM PYTHON SOURCE LINES 8-62

.. code-block:: Python
   :lineno-start: 9


    import pytest
    from packaging.version import parse as parse_version
    from sqlalchemy import __version__ as SA_VERSION

    try:
        from sqlalchemy.orm import DeclarativeBase
        from sqlalchemy.orm import Mapped
        from sqlalchemy.orm import mapped_column
    except ImportError:
        pass

    from geoalchemy2 import Geometry
    from geoalchemy2 import WKBElement
    from geoalchemy2 import shape


    def check_wkb(wkb, x, y) -> None:
        pt = shape.to_shape(wkb)
        assert round(pt.x, 5) == x
        assert round(pt.y, 5) == y


    @pytest.mark.skipif(
        parse_version(SA_VERSION) < parse_version("2"),
        reason="New ORM mapping is only available for sqlalchemy>=2",
    )
    def test_ORM_mapping(session, conn, schema) -> None:
        class Base(DeclarativeBase):
            pass

        class Lake(Base):
            __tablename__ = "lake"
            __table_args__ = {"schema": schema}
            id: Mapped[int] = mapped_column(primary_key=True)
            mapped_geom: Mapped[WKBElement] = mapped_column(Geometry(geometry_type="POINT", srid=4326))

        Lake.__table__.drop(conn, checkfirst=True)  # type: ignore[attr-defined]
        Lake.__table__.create(bind=conn)  # type: ignore[attr-defined]

        # Create new point instance
        p = Lake()
        p.mapped_geom = "SRID=4326;POINT(5 45)"  # type: ignore[assignment]

        # Insert point
        session.add(p)
        session.flush()
        session.expire(p)

        # Query the point and check the result
        pt = session.query(Lake).one()
        assert pt.id == 1
        assert pt.mapped_geom.srid == 4326
        check_wkb(pt.mapped_geom, 5, 45)


.. _sphx_glr_download_gallery_test_orm_mapped_v2.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: test_orm_mapped_v2.ipynb <test_orm_mapped_v2.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: test_orm_mapped_v2.py <test_orm_mapped_v2.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: test_orm_mapped_v2.zip <test_orm_mapped_v2.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
