在 Python Elixir 中编写更新语句
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试从对象的角度思考,而不是 SQL,这就是使用 ORM 的原因。
我几乎只是按照 elixir 教程 进行课程学习:
在 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:
In SQLAlchemy, Python's
None
maps to aNULL
in SQL: Common Filter Operators您可以使用 SQLAlchemy 方式来执行此操作,请注意,elixir 并不打算取代 SQLAlchemy 在许多情况下的工作方式。
You can just use the SQLAlchemy way to do this, note that elixir does not intended to replace how SQLAlchemy works in many case.
如果有人偶然发现这个问题,这应该在 Elixir 中有效:
In case sombody stumbles upon this question, this should work in Elixir: