Which model should contain a SQLalchemy database column to store an array of data?

So, I'm trying to set up a database whose rows will change frequently. Every hour, for example, I want to add a number to a specific part of my database. So, if self.checkmarks is entered into a database equal to 3 , what is the best way to update this part of the database with the added number to make self.checkmarks now equal to 3, 2 ? I tried setting the column as db.Array , but got an attribute error:

AttributeError: SQLAlchemy object does not have 'Array' attribute

I have found how to update the database, but I do not know what is the best way to update it by adding to the list rather than replacing. My approach was as follows, but I do not 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() 

Thank you very much in advance

+4
source share
1 answer

If you really want to have a python list as a column in SQLAlchemy, you need to take a look at PickleType :

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

Note that you will need to use the mutable=True parameter to edit the column. SQLAlchemy will automatically detect the changes and they will be saved immediately after they are committed.

If you want the brine to be convenient for the person, you can combine it with json or other converters sufficient for your purposes.

+5
source

All Articles