Falcoa
Falcoa

Reputation: 2720

sqlacodegen issue with geometry type

i'm using sqlacodegen in order to automatically generate model code for SQLAlchemy.

My problem is that I have some columns of type geometry and as far as I know, according to the link bellow, sqlacodegen does not support this kind of types.

https://bitbucket.org/agronholm/sqlacodegen/issues/18/did-not-recognize-type-geometry-on

Does anyone know how can I workaround this holdback? Maybe with the method Mapper or something?

Thnks

Upvotes: 3

Views: 3374

Answers (1)

iled
iled

Reputation: 2170

Probably you have already figured this out, so this goes to the posterity.

According to the same link you gave (issue #18), this already has a PR to fix it (with no tests though, so it was not accepted as of today).

Update: this PR has been merged as of version 2.1.0. Therefore, the below solution---from the original answer---should not be needed anymore. Note that this PR explicitly addressed the support for PostGIS and not MySQL.


Nevertheless, the fix is easy. Install GeoAlchemy2 (it brings PostGIS support to SQLAlchemy) and import it in sqlacodegen/codegen.py

from sqlalchemy.types import Boolean, String
import sqlalchemy
from geoalchemy2 import Geometry

Otherwise, ignore those SAWarnings and manually fix the geometry types that were not correctly identified, i.e., replace NullType with Geometry.

the_geom = Column(NullType, index=True)
# becomes
the_geom = Column(Geometry(...), index=True)

Upvotes: 5

Related Questions