如何使用f表达式首先将字符串施放到int,然后将1添加到其上,然后施放到字符串和更新

发布于 2025-02-13 19:40:22 字数 1036 浏览 1 评论 0 原文

我有一个DB列,该列是某些统计数据的通用类型(定性和定量信息)。 有些值是字符串 - a型,有些值将数字存储为字符串 - 类型B。 我想做的是将B类型投入数字,然后将其添加到它们中,然后施放回弦并存储。

Metadata.objects.filter(key='EVENT', type='COUNT').update(value=CAST(F(CAST('value', IntegerField()) + 1), CharField())

我想做的是避免使用F表达和 在db。

中更新

https://docs.djangoproject.com/en/4.0/ref/ref/models/models/pexpressions/#avoiding-race-conditions-using-f

它在下面的文章中说,可以在DB中进行铸造和更新DB对于mysql mysql类型casting在更新查询中 我也知道,我们可以在F表达式上很容易地进行算术,因为它支持它,并且我们也可以覆盖add的功能。 在django'f'类型上进行ArthMetic?

如何 我可以实现铸造 - >更新 - >铸造 - >存储在django queryset?

I have a DB column which is generic type for some stats(qualitative and quantitative info).
Some values are string - type A and some values are numbers stored as string - type B.
What i want to do is cast the B types to number then add one to them and cast back to string and store.

Metadata.objects.filter(key='EVENT', type='COUNT').update(value=CAST(F(CAST('value', IntegerField()) + 1), CharField())

What i want to do is avoid race conditions using F expression and
update in DB.

https://docs.djangoproject.com/en/4.0/ref/models/expressions/#avoiding-race-conditions-using-f

It says in below post that casting and updating in db is possible for mysql
Mysql Type Casting in Update Query
I also know we can do arithmetic very easily on F expressions as it supports it and we can override functionality of add as well. How to do arthmetic on Django 'F' types?

How can i achieve Cast -> update -> cast -> store in Django queryset?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

从﹋此江山别 2025-02-20 19:40:22

尝试使用注释如下:

Metadata.objects
    .filter(key='EVENT', type='COUNT')
    .annotate(int_value=CAST('value', IntegerField()))
    .update(value=CAST(F('int_value') + 1, CharField())

或者可以切换f和cast有效?

Metadata.objects
    .filter(key='EVENT', type='COUNT')
    .update(value=CAST(     # cast the whole expression below
        CAST(                 # cast a value
            F('value'),       # of field "value"
            IntegerField()    # to integer
        ) + 1,                # then add 1
        CharField()         # to char.
    )

我增加了凹痕,有时会找到错误。

另外, doc 说,Cast Cast接受字段名称,不是F-Object。也许根本没有F-Object的工作?

upd:切换回第一个示例,它实际上有效:)

Try using annotation as follows:

Metadata.objects
    .filter(key='EVENT', type='COUNT')
    .annotate(int_value=CAST('value', IntegerField()))
    .update(value=CAST(F('int_value') + 1, CharField())

Or maybe switching F and CAST works?

Metadata.objects
    .filter(key='EVENT', type='COUNT')
    .update(value=CAST(     # cast the whole expression below
        CAST(                 # cast a value
            F('value'),       # of field "value"
            IntegerField()    # to integer
        ) + 1,                # then add 1
        CharField()         # to char.
    )

I've added indentation, it helps sometimes to find the errors.

Also, doc says, CAST accepts field name, not an F-object. Maybe it works without F-object at all?

UPD: switched back to first example, it actually works :)

落叶缤纷 2025-02-20 19:40:22

我相信 @SOM-1的答案是有益的,但没有通过信息或调试数据证实。我相信假设并不总是正确的。

我调试了这两种情况下形成的mySQL查询

-1- metadata.objects.update(value = cast(cast(f cast('value'),output_field = integerfield()) + 1,output_field = charfield = charfield()) )

2- metadata.objects.update(value = cast(cast('value',integerfield()),integerfield()) + 1, charfield()))
两者都给出与预期的相同输出。

UPDATE Metadata SET value = CAST((CAST(value AS signed integer) + 1) AS char) WHERE ( key = 'EVENT' AND  type = 'COUNT' )

请找到将mysqld选项添加到我的cnf并调试查询的链接。 https .com/blog/2018/10/how-to-show-queries-log-in-mysql.html

I believe the answer from @som-1 was informative but not substantiated with info or debugged data. I believe assuming is not always right.

I debugged the mysql queries formed in these two cases -

1 - Metadata.objects.update(value=Cast(Cast(F('value'), output_field=IntegerField()) + 1, output_field=CharField()))

2 - Metadata.objects.update(value=Cast(Cast('value', IntegerField()) + 1, CharField())) and
both give the same output as expected.

UPDATE Metadata SET value = CAST((CAST(value AS signed integer) + 1) AS char) WHERE ( key = 'EVENT' AND  type = 'COUNT' )

Please find the link to add mysqld options to my.cnf and debug your queries. Location of my.cnf file on macOS
enabling queries - https://tableplus.com/blog/2018/10/how-to-show-queries-log-in-mysql.html

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