Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试编写一个 alembic 迁移以将日期时间列添加到表中。我希望所有现有行的默认时间为现在,未来的行默认为它们的创建时间。我试过server_default='now()'了,它将所有现有行设置为现在,但新行似乎在同一时间得到。如何设置默认值以便新行获取当前时间?
server_default='now()'
问题是默认设置为的结果,now()而不是它的执行,所以默认将是它设置的确切时间,而不是插入时的当前时间。用于sa.func.current_timestamp()将其设置为该函数,而不是该函数的结果。
now()
sa.func.current_timestamp()
def upgrade(): op.add_column('my_table', sa.Column('my_column', sa.DateTime, server_default=sa.func.current_timestamp()))