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?

Ben
  • 51,770
  • 36
  • 127
  • 149
mlzboy
  • 14,343
  • 23
  • 76
  • 97

3 Answers3

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)
Jaimy Azle
  • 138
  • 2
  • 4
0

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

Product.query.filter(Product.id > 100).update({Product.price: None})
Pankrat
  • 5,206
  • 4
  • 31
  • 37
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

snapshoe
  • 13,454
  • 1
  • 24
  • 28