Can I call function in SQLAlchemy like this?
db.session.query(User).update({'spotify_date':format_date(User.spotify_date)})
db.session.commit()
Here I tried to call function format_date() to format date in column spotify_date of relation table User.
Here is user models
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
spotify_id = db.Column(db.String(20), unique=False, nullable=True)
spotify_token = db.Column(db.String(20), unique=False, nullable=True)
spotify_date = db.Column(db.String(20), unique=False, nullable=True)
And function fomate_date() and call of function
from app import app, db, User
def format_date(date):
"""Format date to local format: dd-mm-yyyy
Args:
date (String): spotify_date
Returns:
string: date formatted in local
Examples:
>>> formate_date('2020-07-18')
'18-07-2020'
"""
dateList = date.split('-')
dateList.reverse()
return '-'.join(dateList)
db.session.query(User).update({'spotify_date':format_date(User.spotify_date)})
db.session.commit()
After that I got an exception as:
AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with User.spotify_date has an attribute 'split'
Anyway, how I can correctly do this in SQLAlchemy query statement? Thanks
P.S: The database I am using is Postgresql; the function is the python function, not database function. Thanks.