Hi Michael,
On Monday, September 7, 2015 at 5:40:02 PM UTC-4, Michael Bayer wrote: > > SQLAlchemy doesn't do much else with types at the most basic level other > than pass the data through to the DBAPI, in this case hopefully psycopg2. > Feel free to set the column type to NullType and just pass through strings, > assuming that's what psycopg2 does here by default, or if you'd like to > define your own type that translates some Python value to what psycopg2 > expects here, there is UserDefinedType as well as TypeDecorator: > http://docs.sqlalchemy.org/en/rel_1_0/core/custom_types.html#types-custom > That's perfect, thanks. I decided to try a TypeDecorator, and it's working for me. For anyone else who comes across this, I did this: class Field(Base): __tablename__ = 'field' __table_args__ = {'autoload' : True} polygon_ = Column('polygon', PGPolygon) The column name in the database is "polygon"; I couldn't figure out how to override it after autoload, so I created a new column property mapped to the same column. (I'd be happy to hear a better way to do this.) I then created a new file that handles the translation (pasted below). One should be able to easily expand this to add the other native PostgreSQL geometric data types. Cheers, Demitri --- ''' These classes define native PostgreSQL geometry types to be used with SQLAlchemy. Ref: http://www.postgresql.org/docs/9.4/static/datatype-geometric.html ''' from sqlalchemy.types import TypeDecorator, VARCHAR class PGPolygon(TypeDecorator): """Represents the native polygon data type in PostgreSQL (i.e. *not* PostGIS). Usage:: polygonColumnName = Column('polygon_column_name', PGPolygon) """ impl = VARCHAR def process_bind_param(self, value, dialect): ''' Take the object and convert it into a string to be entered into the database. The value should be in the form of a Python list of tuples, e.g. [ (x1,y1), (x2,y2), (x3,y3), ... ] ''' if value is not None: value = "({0})".format(",".join([str(x) for x in value])) return value def process_result_value(self, value, dialect): ''' Take the polygon value from the database and convert it into a list of point tuples. The incoming format looks like this: '((12,34),(56,78),(90,12))' ''' if value is not None: polygon = list() for point in value[1:-1].split("),("): # also strip outer single quotes point = point.lstrip("(") # remove extra "(" ")" (first and last elements only) point = point.rstrip(")") x, y = point.split(",") polygon.append((float(x), float(y))) value = polygon return value -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
