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),


Flask – Postgis and Alembic Migration

2 thoughts on “Flask – Postgis and Alembic Migration

  • January 14, 2020 at 3:18 pm

    You’re godsend. Thank you very much for this blog post. I was getting lost trying to solve the issue.

    I’ve added the error below. So anybody typing the error in Google to find out the solution can come to this page.

    Here is the error I’ve got:
    (psycopg2.errors.DependentObjectsStillExist) cannot drop table spatial_ref_sys because extension postgis requires it
    Hint: You can drop extension postgis instead


Leave a Reply

Your email address will not be published. Required fields are marked *

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