I am using Postgres DB with SQLAlchemy as toolkit.
My model looks like
class Test(db.Model):
"""Test entry in DB."""
__tablename__ = 'test'
key1 = db.Column(db.String, primary_key=True)
key2 = db.Column(postgresql.ARRAY(JSON))
I want to know how can I add elements to my key2
column which is an array?
For making changes to the DB, let's say I am using something of this kind:
test = Test(key1="key2", key2=array)
db.session.add(test)
How can I add new elements to this array or make a new array and assign it to key2
? Basically struggling with proper documentation for JSON Arrays in Postgres/SQLAlchemy.
Any help would be highly appreciated.
Column(JSON)
column as well, but JSON lists do not support slicing, if that is required. With your current model you can use an explicit CAST-expression:test = Test(key1="key2", key2=sqlalchemy.cast(array, postgresql.ARRAY(JSON)))
, but that is a bit ugly. – Ilja Everilä Oct 13 at 5:10