I'm trying to change a table column in PostgreSQL using Alembic but I don't know how to perform the needed update to apply the SQLAlchemy's server_onupdate property.
The column is:
changed = Column(ArrowType(timezone=True), server_default=utcnow(), primary_key=True)
I'm using the Arrowtype column type from SQLAlchemy_utils package (this is not a problem).
My intention is to create something like this:
changed = Column(ArrowType(timezone=True), **server_onupdate=utcnow()**, primary_key=True)
But using the Alembic function: alter_column
In the documentation there are only references to the server_default property but nothing about server_onupdate
Is there a way to achieve this?
Thanks
To change the server_onupdate property of a column using Alembic, you can use the server_onupdate argument in the alter_column function, like this:
from alembic import op from sqlalchemy import Column, ArrowType from sqlalchemy_utils import utcnow def upgrade(): op.alter_column('table_name', 'changed', server_onupdate=utcnow(), existing_type=ArrowType(timezone=True), existing_server_default=utcnow(), existing_nullable=False)
This will change the server_onupdate property of the changed column to utcnow() in the table_name table.
Note that you will also need to provide the existing data type and server default value of the column using the existing_type and existing_server_default arguments, respectively. This is because Alembic needs to know the current state of the column in order to update it.