2010-10-19 99 views
0

我使用的药剂作为我的ORM的MySQL数据库,我在写一个更新语句,比如问题:写update语句

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

我应该怎么做?

+0

如何模型类(ES)定义的? – snapshoe 2010-10-19 03:18:15

+0

@ ma3204该类定义如上所列 – mlzboy 2010-10-19 03:20:46

回答

0

试着用对象来思考,而不是SQL,这就是拥有ORM的原因。
我非常简单,只是跟着elixir tutorial与你的类:

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

在SQLAlchemy中,Python的None映射到一个NULL在SQL:Common Filter Operators

+0

性能非常差 – mlzboy 2010-10-19 05:11:43

0

您可以只使用SQLAlchemy的方式做到这一点,需要注意的是elixir不打算取代SQLAlchemy在很多情况下的工作方式。

import sqlalchemy as sa 

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

如果sombody绊倒在这个问题,这应该在药剂工作:

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