In SQL Alchemy, how do I set a default depending on another column which is set with a default value? I need to create a random uuid and assign it to the new user. I then need to create a default contact url that is f'{base_url}/{user_uuid}'. Unlike in SQLAlchemy set default value of one column to that of another column, get_current_parameters does not work here: it cannot access the uuid, as it is not a parameter (and even then it would not be enough, as I would need to prefix it with the base url).
My code so far:
def same_as(column_name: str) -> Callable:
"""
Return function that calls default value from column_name in same table.
param column_name: str valid column of table
:return:
"""
def sql_alchemy_default(context):
return context.get_current_parameters()[column_name]
return sql_alchemy_default
def create_uuid() -> str:
"""
Generate random unique str.
:return: str
"""
new_uuid = secrets.token_urlsafe(8).lower()
if db.session.query(User.user_uuid).filter_by(user_uuid=new_uuid).first() is not None:
return create_uuid()
return new_uuid
class User(UserMixin, db.Model):
user_uuid: str = db.Column(db.String(11), default=lambda: create_uuid(), index=True, unique=True)
contact_url: str = db.Column(db.String(120), default=same_as('user_uuid'))
user = User() assert user.contact_url == 'base_url' + user.user_uuid # will raise error