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
- Edit the migration script files under migration folder.
- Add import from geoalchemy2.types import Geometry
- Comment op.drop_table(‘spatial_ref_sys’)
- 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
Thanks
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