You can use MutableList
from sqlalchemy
from sqlalchemy.ext.mutable import MutableList
from sqlalchemy import pickleType
import logging # not necessary, for logging purpose only
my_list_column_field = Column(MutableList.as_mutable(PickleType),
default=[])
# now you can do common operations in this list. e.g:
# append to the list (just like python)
my_list_column_field = my_list_column_field + [any_data_type]
# and don't forget to commit your changes
try:
db.commit()
except SQLAlchemyError as e:
db.rollback()
logging.error("Failed to Commit because of {error}. Doing Rollback".format(error=e))
If you simply use PickleType
, you won't be able to modify that list after the first insert. To modify later we need MutableList
. You can also use Set, Dict
like this using MutableSet, MutableDict
.
There is another less obvious way though. To save as String by json.dumps(my_list)
and then while retrieving just do json.loads(my_column)
. But it will require you to set the data in a key-value format and seems a bit in-efficient compared to the previous solution.