What model should a SQLalchemy database column be to contain an array of data?


Question

So I am trying to set up a database, the rows of which will be modified frequently. Every hour, for instance, I want to add a number to a particular part of my database. So if self.checkmarks is entered into the database equal to 3, what is the best way to update this part of the database with an added number to make self.checkmarks now equal 3, 2? I tried establishing the column as db.Array but got an attribute error:

AttributeError: 'SQLAlchemy' object has no attribute 'Array'

I have found how to update a database, but I do not know the best way to update by adding to a list rather than replacing. My approach was as follows, but I don't think append will work because the column cannot be an array:

ven = data.query.filter_by(venid=ven['id']).first()
ven.totalcheckins = ven.totalcheckins.append(ven['stats']['checkinsCount'])
db.session.commit()

Many thanks in advance

1
5
11/25/2012 6:15:25 AM

If you really want to have a python list as a Column in SQLAlchemy you will want to have a look at the PickleType:

array = db.Column(db.PickleType(mutable=True))

Please note that you will have to use the mutable=True parameter to be able to edit the column. SQLAlchemy will detect changes automatically and they will be saved as soon as you commit them.

If you want the pickle to be human-readable you can combine it with json or other converters that suffice your purposes.

6
11/27/2012 1:36:36 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon