I'm trying to write an alembic migration to add a datetime column to a table. I want all existing rows to have a default time of right now and future rows to default to the time they were created.  I've tried server_default='now()', which sets all existing rows to right now, but new rows seem to get this same time.  How do I set the default so new rows get the current time?
The issue is that the default is set to the result of now(), not the execution of it, so the default will be the exact time it was set, rather than the current time at insert.  Use sa.func.current_timestamp() to set it to that function, rather than the result of that function.
def upgrade():
    op.add_column('my_table', sa.Column('my_column', sa.DateTime,
            server_default=sa.func.current_timestamp()))
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With