将null插入SQL Alchemy DateTime
我正在进行一个迁移,其中有一个我将表A分裂为表B和C。我的(简化的)迁移脚本是:
from alembic import op
con = op.get_bind()
items = con.execute("SELECT * FROM public.A")
for item in items:
B_id = item.b
C_id = item.c
some_attributes = item.some_attributes
some_other_attributes = item.some_other_attributes
created_at = item.created_at
updated_at = created_at
if item.deleted:
deleted_at = created_at
else:
deleted_at = created_at # This should be None
op.execute("INSERT INTO B (id, some_attributes, created_at, updated_at, deleted_at)\
VALUES ('{}','{}','{}','{}','{}')\
ON CONFLICT DO NOTHING".format(B_id, some_attributes, created_at, updated_at, deleted_at))
op.execute("INSERT INTO C (id, some_other_attributes ,created_at, updated_at, deleted_at, B_id)\
VALUES ('{}','{}','{}','{}','{}','{}')".format(C_id,some_other_attributes,created_at,updated_at,deleted_at,B_id))
目前有效,但事实是,当我的物品未删除时,我会喜欢我的<代码> deleted_at 要设置为null
。问题是,如果我将值设置为none,则“ null”
,“ null”
或sqlalchemy.sql.null()
i获取此错误:
sqlalchemy.exc.DataError: (psycopg2.errors.InvalidDatetimeFormat) invalid input syntax for type timestamp: "NULL"
我的列是无效的,我可以通过在if item.deleted
语句中复制我的代码来完成我想做的事情将其设置为null,但这一点一点都不优雅。
我也可以从模型中导入A,B和C。我只是为了迁移而这样做。
有人对我有任何想法吗?告诉我是否需要编辑任何内容以添加精确度。
I am doing a migration where I have a table A that I'm splitting into table B and C. My (simplified) migration script is :
from alembic import op
con = op.get_bind()
items = con.execute("SELECT * FROM public.A")
for item in items:
B_id = item.b
C_id = item.c
some_attributes = item.some_attributes
some_other_attributes = item.some_other_attributes
created_at = item.created_at
updated_at = created_at
if item.deleted:
deleted_at = created_at
else:
deleted_at = created_at # This should be None
op.execute("INSERT INTO B (id, some_attributes, created_at, updated_at, deleted_at)\
VALUES ('{}','{}','{}','{}','{}')\
ON CONFLICT DO NOTHING".format(B_id, some_attributes, created_at, updated_at, deleted_at))
op.execute("INSERT INTO C (id, some_other_attributes ,created_at, updated_at, deleted_at, B_id)\
VALUES ('{}','{}','{}','{}','{}','{}')".format(C_id,some_other_attributes,created_at,updated_at,deleted_at,B_id))
For now this works but the thing is that when my item is not deleted I would like my deleted_at
to be set to NULL
. The thing is that if I set the value to None, "NULL"
, "null"
or even sqlalchemy.sql.null()
I get this error :
sqlalchemy.exc.DataError: (psycopg2.errors.InvalidDatetimeFormat) invalid input syntax for type timestamp: "NULL"
My column is nullable, I can do what I want by duplicating my code in the if item.deleted
statement and not mention the deleted _at
column when I want to set it to null but that's not very elegant at all.
I could also import A, B and C from my models and create a new instance of B and C each time and add then commit each one to my session, but this would require me to change B and C init code and that feels wrong to me to do so just for migration sake.
Does anyone have any idea for me ? Tell me if I need to edit anything to add precisions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于通过这种方法来做到这一点:
但是,我没有更改B和C的初始代码:我只是这样做了:
I finally managed to do it with this approach :
But instead of changing the init code for B and C i just did this :