在Python Elixir中编写更新语句
我正在使用Elixir作为我的MySQL数据库的对象关系映射工具(ORM),在写更新语句时遇到了一些问题,比如:
"update products set price=NULL where id>100"
这是我的Elixir代码
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))
我应该怎么做呢?
3 个回答
0
如果有人碰巧看到这个问题,这段代码在Elixir中应该可以正常工作:
Product.query.filter(Product.id > 100).update({Product.price: None})
0
你可以直接用SQLAlchemy的方法来做到这一点。需要注意的是,elixir并不是为了替代SQLAlchemy在很多情况下的工作方式。
import sqlalchemy as sa
sa.update(Product.table).\
where(Product.id > 100).\
values(price=None)