I have a column "value" with type postgres 'JSON'. Example data in the value column.
- "displayname1"
- {"middleName": "other"}
- [{"primary": true, "type": "work", "value": "rusr@r.com"}, {"primary": true, "type": "work", "value": "iu@n.com"}]
I know i can access json with key-value pairs like this,
.filter(Values.value['middleName'].astext=='other')(values is model name)
the question is how do you access string value or array of objects.
i tried,
.filter(Values.value.astext=='displayname1')
it gives me AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Values.value has an attribute 'astext'
and
.filter(Values.value['type'].astext=='work')- here i am trying filter "type":"work" objects in the array. How do you do this. Is it possible to search in all objects with out specifying the index.
I am using postgres as backend Db.
Models -
class Values(db.Model):
__tablename__ = 'attribute_values'
id = db.Column(db.Integer, primary_key=True)
attribute = db.Column(db.Integer, ForeignKey('attributes.id'), primary_key=True)
value = db.Column(JSON)
class Attributes(db.Model):
__tablename__ = 'attributes'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text)
user_attribute = db.relationship(Values, backref='attributes', primaryjoin=id == Values.attribute)
query that i am trying -
db.session.query(Values).join(Attributes, Values.attribute == Attributes.id).filter("what filters that i should be using for the cases mentioned above").all()