sqlathanor icon indicating copy to clipboard operation
sqlathanor copied to clipboard

Cant pass a text function for server_default in kwargs

Open javivallejo opened this issue 3 years ago • 1 comments

When defining a column_kwargs dict to define a Table, there is no way to pass a text() function to be rendered as it is, as defined in the SQLAlchemy docs here

I cannot make it work to pass a server_default value of CURRENT_TIMESTAMP in order to make the DB assign this value.

Am I doing something wrong or is it an issue?

javivallejo avatar Apr 23 '22 14:04 javivallejo

I've solved with a schema.py modification to handle this special case but for sure it's not the best way, just sharing here if it can help anyone:

Note: it is also needed to import text from sqlalchemy

           additional_kwargs = column_kwargs.get(key, {})
            for kwarg in additional_kwargs:
                if isinstance(additional_kwargs[kwarg], str) and additional_kwargs[kwarg][0:5] == 'text(':
                    column_dict[kwarg] = text(additional_kwargs[kwarg][5:-1])
                else:
                    column_dict[kwarg] = additional_kwargs[kwarg]

            column = Column(**column_dict)
            columns.append(column)

This way, if I set text(current_timestamp) in the column_kwargs, it will be handled to text function (anything inside text()). All the rest keeps being a string literal.

javivallejo avatar Apr 23 '22 15:04 javivallejo