I need to write up things just for remembering how I solved a particular issue if occurs in the future.
Sqlite (with http://sqlitebrowser.org) is great for rapid prototypes development but it lacks some useful implementations provided by Postgresql (for example the sqlalchemy.dialects.postgresql.ARRAY type).
I solved implementing a SQLAlchemy TypeDecorator with a json serialization:
Here it is the self-explaining code:
Sqlite (with http://sqlitebrowser.org) is great for rapid prototypes development but it lacks some useful implementations provided by Postgresql (for example the sqlalchemy.dialects.postgresql.ARRAY type).
I solved implementing a SQLAlchemy TypeDecorator with a json serialization:
Here it is the self-explaining code:
from sqlalchemy.schema import ColumnIf you are not using Pyramid just replace the pyramid_sqlalchemy's Base wrapper import with:
from sqlalchemy.types import (
Integer,
String,
TypeDecorator,
)
from sqlalchemy import Sequence
from pyramid_sqlalchemy import BaseObject as Base
import json
class ArrayType(TypeDecorator):
""" Sqlite-like does not support arrays.
Let's use a custom type decorator.
See http://docs.sqlalchemy.org/en/latest/core/types.html#sqlalchemy.types.TypeDecorator
"""
impl = String
def process_bind_param(self, value, dialect):
return json.dumps(value)
def process_result_value(self, value, dialect):
return json.loads(value)
def copy(self):
return ArrayType(self.impl.length)
class Element(Base):
__tablename__ = 'elements'
id = Column(Integer(),
Sequence('element_id_seq'),
primary_key = True)
# ...
myarray = Column(ArrayType())
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()