如何迁移具有新日期时间列(不可为空)的表中数据的表? [英] How can I migrate a table with data in a table which got a new datetime column which is non-nullable?
本文介绍了如何迁移具有新日期时间列(不可为空)的表中数据的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已添加一列
registered_on = db.Column(db.DateTime, nullable=False)
到我的users
表.自动创建的迁移是
to my users
table. The migration which was automatically created is
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
...
op.add_column('users', sa.Column('registered_on', sa.DateTime(), nullable=False))
...
当我执行它时,我得到了一个关于无效值0000-00-00 00:00:00
的异常(等等).
When I execute it, I got an exception about an invalid value 0000-00-00 00:00:00
(or so).
我应该如何调整迁移脚本以解决此问题?
How should I adjust the migration script to not have this problem?
(在这种情况下,最好填写一个虚拟值)
(In this situation, filling in a dummy value would probably have been the best)
推荐答案
我可以想到两种方法:
- 在
add_column
操作中添加server_default
:
- Add a
server_default
to theadd_column
operation:
op.add_column('users', sa.Column('registered_on', sa.DateTime(), nullable=False, server_default=func.now()))
- 首先将列插入为可空值,然后将所有行编辑为具有值.给所有行赋予非空值后,再进行一次迁移以将列更改为非空.
这篇关于如何迁移具有新日期时间列(不可为空)的表中数据的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文