Hi,
I've been struggling for a while trying to create a mapping that works with both PostGIS and SQLite databases when dealing with DateTime.
Background: Production PostGIS database has been reduced and converted to a spatialite database with the same schema for offline testing purposes.
Problem: PostGIS TIMESTAMP is now stored as SQLite TIMESTAMP, but effectively as a string. I think the main problems is that the default SQLite DateTime dialect storage_format includes miliseconds, our data does not.
A solution which seems to be working is to declare a TypeDecorator and set the Column type to it:
class SQLiteDateTime(types.TypeDecorator):
impl = types.String
def process_bind_param(self, value, dialect):
return datetime.strftime(value, '%Y-%m-%dT%H:%M:%S')
def process_result_value(self, value, dialect):
return datetime.strptime(value, '%Y-%m-%dT%H:%M:%S')
This works as expected for SQLite, however does not for PostGIS (unless checking for
dialect.name = 'sqlite'...)
Overriding type compilation seems to be exactly what I need, however, as per the example:
@compiles(BINARY, "sqlite")
def compile_binary_sqlite(type_, compiler, **kw):
return "BLOB"
Changing to:
@compiles(DateTime, "sqlite")
def compile_datetime_sqlite(type_, complier, **kw):
return "SQLiteDateTime"
Never runs as checked with an assert=False.
Currently these are both at the top of a mapping file which all the tables are declared. Am I just missing something about compile time overrides?
Thanks,
Basil