Hi,

I'm using sqlalchemy with postgresql-9.3. I'm trying to create a query-able 
column in a table that checks for explicit user overrides before checking 
the original "raw" data in a JSON column.

An example of what I'm trying to do is below.

The 'raw_data' JSON may be something like: {'foo': 'bar', 'value': 2}. First, 
the 'override' column, '_value' is checked for a non-None value, and then 
the JSON is checked for key 'value', and then None is returned as the 
default.

class MyModel(db.Model):
    __tablename__ = 'my_model'
    
    id = db.Column(db.Integer, primary_key=True)

    _value = db.Column(db.Integer)

    @hybrid_property
    def value(self):
        if self._value is not None:
            return self._value
        elif 'value' in self.raw_data and self.raw_data['value'] is not 
None:
            return self.raw_data['value']
        else:
            return None
    
    raw_data = db.Column(JSON)

I think hybrid_property is the way to do this, and the 'value' property 
works on the instance level, but I can't seem to query on the 'value' 
property, like so:

MyModel.query.filter(MyModel.value > 0).all()

Is there a good way to get the functionality I am looking for, as well as 
index on the 'value' hybrid_property? Keeping the 'raw_data' JSON column as 
is would be preferable, since I plan to refresh that single column 
periodically from an external source.

Thanks!

K

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to