在 Python Elixir 中编写更新语句

发布于 2024-09-28 16:04:17 字数 1776 浏览 4 评论 0原文

我正在使用 Elixir 作为 MySQL 数据库的 ORM,我在编写更新语句时遇到问题,例如:

"update products set price=NULL where id>100"

这是我的 Elixir clsee

class Product(Entity):
    using_options(tablename='model_product')
    name                        = Field(Unicode(200))
    en_name             = Field(Unicode(200))
    price                       = Field(Float)
    en_price            = Field(Float)
    productid                   = Field(Unicode(200))
    site                        = Field(Unicode(30))
    link                        = Field(Unicode(300))
    smallImage                  = Field(Unicode(300))
    bigImage                    = Field(Unicode(300))
    description                 = Field(UnicodeText)
    en_description              = Field(UnicodeText)
    createdOn                   = Field(DateTime, default=datetime.datetime.now)
    modifiedOn                  = Field(DateTime, default=datetime.datetime.now)
    size                        = Field(Unicode(30))
    en_size                     = Field(Unicode(30))
    weight                      = Field(Unicode(30))
    en_weight                   = Field(Unicode(30))
    wrap                        = Field(Unicode(30))
    en_wrap                     = Field(Unicode(30))
    material                    = Field(Unicode(30))
    en_material                 = Field(Unicode(30))
    packagingCount              = Field(Unicode(30))
    stock                       = Field(Integer)
    location                    = Field(Unicode(30))
    en_location                 = Field(Unicode(30))
    popularity                  = Field(Integer)
    inStock                     = Field(Boolean)
    categories                  = Field(Unicode(30))

我应该如何执行此操作?

I'm using Elixir as my ORM for a MySQL database, I'm having problems writing an update statement, for instance:

"update products set price=NULL where id>100"

This is my Elixir clsee

class Product(Entity):
    using_options(tablename='model_product')
    name                        = Field(Unicode(200))
    en_name             = Field(Unicode(200))
    price                       = Field(Float)
    en_price            = Field(Float)
    productid                   = Field(Unicode(200))
    site                        = Field(Unicode(30))
    link                        = Field(Unicode(300))
    smallImage                  = Field(Unicode(300))
    bigImage                    = Field(Unicode(300))
    description                 = Field(UnicodeText)
    en_description              = Field(UnicodeText)
    createdOn                   = Field(DateTime, default=datetime.datetime.now)
    modifiedOn                  = Field(DateTime, default=datetime.datetime.now)
    size                        = Field(Unicode(30))
    en_size                     = Field(Unicode(30))
    weight                      = Field(Unicode(30))
    en_weight                   = Field(Unicode(30))
    wrap                        = Field(Unicode(30))
    en_wrap                     = Field(Unicode(30))
    material                    = Field(Unicode(30))
    en_material                 = Field(Unicode(30))
    packagingCount              = Field(Unicode(30))
    stock                       = Field(Integer)
    location                    = Field(Unicode(30))
    en_location                 = Field(Unicode(30))
    popularity                  = Field(Integer)
    inStock                     = Field(Boolean)
    categories                  = Field(Unicode(30))

How should I be doing this?

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

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

发布评论

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

评论(3

丢了幸福的猪 2024-10-05 16:04:17

尝试从对象的角度思考,而不是 SQL,这就是使用 ORM 的原因。

我几乎只是按照 elixir 教程 进行课程学习:

for p in Product.query.filter(Product.id > 100):
    p.price = None
session.commit()

在 SQLAlchemy 中,Python 的 None 映射到 SQL 中的 NULL常用过滤器运算符

Try to think in terms of objects, not SQL, which is the reason for having an ORM.
I pretty much just followed the elixir tutorial with your class:

for p in Product.query.filter(Product.id > 100):
    p.price = None
session.commit()

In SQLAlchemy, Python's None maps to a NULL in SQL: Common Filter Operators

国际总奸 2024-10-05 16:04:17

您可以使用 SQLAlchemy 方式来执行此操作,请注意,elixir 并不打算取代 SQLAlchemy 在许多情况下的工作方式。

import sqlalchemy as sa

sa.update(Product.table).\
    where(Product.id > 100).\
    values(price=None)

You can just use the SQLAlchemy way to do this, note that elixir does not intended to replace how SQLAlchemy works in many case.

import sqlalchemy as sa

sa.update(Product.table).\
    where(Product.id > 100).\
    values(price=None)
两个我 2024-10-05 16:04:17

如果有人偶然发现这个问题,这应该在 Elixir 中有效:

Product.query.filter(Product.id > 100).update({Product.price: None})

In case sombody stumbles upon this question, this should work in Elixir:

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