0

MySQL データベースの ORM として Elixir を使用しています。更新ステートメントの作成に問題があります。たとえば、次のようになります。

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

これは私のエリクサー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

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

誰かがこの質問に出くわした場合、これは Elixir で動作するはずです:

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

SQL ではなく、オブジェクトの観点から考えるようにしてください。これが ORM を持つ理由です。私はあなたのクラスでエリクサーのチュートリアル
にほとんど従いました:

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

SQLAlchemy では、Pythonは SQLNoneの a にマップさNULLれます:一般的なフィルター演算子

于 2010-10-19T03:46:02.930 に答える