将null插入SQL Alchemy DateTime

发布于 2025-02-07 09:15:54 字数 1484 浏览 4 评论 0原文

我正在进行一个迁移,其中有一个我将表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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法少女 2025-02-14 09:15:54

我终于通过这种方法来做到这一点:

我也可以从我的模型中导入A,B和C,并每次创建B和C的新实例,然后将每个实例添加到我的会话中,但这需要我更改B和C INIT代码,并且只是为了迁移而感到不对劲。

但是,我没有更改B和C的初始代码:我只是这样做了:

b = B(some_attributes,created_at)
b.deleted_at = deleted_at
session.add(b)
session.commit()

I finally managed to do it with this approach :

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.

But instead of changing the init code for B and C i just did this :

b = B(some_attributes,created_at)
b.deleted_at = deleted_at
session.add(b)
session.commit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文