Search code examples
pythonpostgissqlmodelgeoalchemy2

Use PostGIS geometry types in SQLModel


Is it possible to use PostGIS geometry types in models created in SQLModel? If yes, how can it be done?


Solution

  • Yes it is.

    Just take advantage of the sqlalchemy package that makes the work underneath SQLModel and the the GeoAlchemy2 package which is compatible with sqlalchemy and has already defined geometry types.

    So,

    from geoalchemy2 import Geometry
    from sqlmodel import SQLModel, Field, Column
    
    class Record(SQLModel, table=True):
        point: Any = Field(sa_column=Column(Geometry('POINT'))) # Here POINT is used but could be other geometries as well