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))

我应该怎么做?

4

3 回答 3

0

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

Product.query.filter(Product.id > 100).update({Product.price: None})
于 2015-04-28T09:51:09.560 回答
0

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

import sqlalchemy as sa

sa.update(Product.table).\
    where(Product.id > 100).\
    values(price=None)
于 2013-07-29T03:40:09.133 回答
0

尝试从对象而不是 SQL 的角度来思考,这就是拥有 ORM 的原因。
我几乎只是在您的课程中遵循了长生不老药教程

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

在 SQLAlchemy 中,PythonNone映射到NULLSQL 中的 a:通用过滤器运算符

于 2010-10-19T03:46:02.930 回答