Post tagged: SQL

SQLAlchemy `order_by` gotcha

Today I encountered a surprising SQLAlchemy gotcha. In short, if you have a Query object and call its order_by method on the same column more than once, the first ORDER BY is not superseded. Here's an illustrative example. I'll include the boilerplate code (schema, etc.) at the end of the …

Dynamically generating SQLAlchemy tables

Background

Here's the background: I had some structured data that I wanted to log to a database. That took the form of Python classes something like this:

class Message:
    code = 0x00
    endianness = '!'
    payload_format = {
        'device_address': 'L',
        'packet_id': 'L',
        'length': 'L',
        'enable': 'L',
    }

    def __init__(self, **kwargs):
        self.fields = kwargs

I wrote …