So it turns out SQLAlchemy doesn’t really smooth over differences between
databases that much — whoops! Maybe you, like me, need to write queries that
work across several engines (i.e., and so dialects) like SQLite, PostgreSQL,
etc.
Here’s a semi-nice way I found to do that using the
SQLAlchemy’s compilation extension API
which avoids the hassle of having dialect-specific database classes, etc.
After defining this new expression element:
You can switch out different expressions based on dialect, where the kwargs for
the expression match a particular dialect or default otherwise, like so:
And here’s the output once compiled with those various dialects:
You can use it anywhere else you’d put a column. Here’s a more
concrete example, which is what I needed it for: