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

Models

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

 

Error

 

Workaround

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

 

2 comments

  1. Thanks

    Reply
  2. 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:
    sqlalchemy.exc.InternalError
    (psycopg2.errors.DependentObjectsStillExist) cannot drop table spatial_ref_sys because extension postgis requires it
    Hint: You can drop extension postgis instead

    Reply

Write a Reply or Comment

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