0

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?

2
  • how are your model class(es) defined?
    – snapshoe
    Oct 19, 2010 at 3:18
  • @ma3204 the class define as above listed
    – mlzboy
    Oct 19, 2010 at 3:20

3 Answers 3

0

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:

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

In SQLAlchemy, Python's None maps to a NULL in SQL: Common Filter Operators

0
0

You can just use the SQLAlchemy way to do this, note that elixir does not intended to replace how SQLAlchemy works in many case.

import sqlalchemy as sa

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

In case sombody stumbles upon this question, this should work in Elixir:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.