Flask – Postgis and Alembic Migration

Flask – Postgis and Alembic Migration

Migration flask application with database postgis type Geometry.

Main Issues

`enter code here`NameError: name 'geoalchemy2' is not defined


from geoalchemy2 import Geometry

class Provider(db.Model):
    __tablename__ = 'provider'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    address = db.Column(db.String(1000))
    geom = db.Column(Geometry('POINT', srid=4326))

    def __repr__(self):
        return '<Provider> {}'.format(self.id)





  1. Edit the migration script files under migration folder.
  2. Add import from geoalchemy2.types import Geometry
  3. Comment op.drop_table(‘spatial_ref_sys’)
  4. Run flask db upgrade command



Remove Package

Lastly, remove the package geoalchemy2.types by using object directly.

sa.Column('geom', geoalchemy2.types.Geometry(geometry_type='POINT', srid=4326), nullable=True),


Change To

sa.Column('geom', Geometry(geometry_type='POINT', srid=4326), nullable=True),


One comment

  1. Thanks


Write a Reply or Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.