Hi All,
I'm trying out SQLAlchemy, And I cant figure out how to update an existing
record without first querying for the object.
I'm using the Flask-SQLAlchemy extension and SQLAlchmeny v0.7.3 with a
Firebird 2.5.1 database
/* my model */
class Supplier(db.Model):
__tablename__ = "supplier"
id = db.Column(
db.BigInteger, db.Sequence('gen_supplier_id'), primary_key=True,
nullable=False
)
name = db.Column(db.String(100), nullable=False)
contact_person= db.Column(db.String(50), nullable=False)
phone= db.Column(db.String(45), nullable=False)
How do i run the equivalent of 'update supplier set name = "mayowa" where
id=2' using sqla?
From what I've read on the internets the prevailing wisdom is to use the
following:
session.query(Supplier).filter_by(id=2).update({"name": u"Mayowa"})
but that first results in a "select" query before the update
there has to be a simple way to do this, but I just cant seem to find it.
Thank you
Mayowa
*Aut viam inveniam aut faciam ...*
Hi! Select the object model you are trying to update (I'll assume that you are using the flask-sqlalchemy extension): Select the item you are trying to update and store it in a variable: supplier = Supplier.query.filter_by(id=[id here]).first() Like any ordinary object in python, you can always access an orm object's properties (classvar.classproperty) and modify its values (classvar.classproperty = newvalue). supplier.name = "mayowa" The commiting the changes db.session.add(supplier) db.session.commit() Here's a really good documentation on how to use the sqlalchemy with flask: http://packages.python.org/Flask-SQLAlchemy/index.html Hope this helped. On Tuesday, 18 October, 2011 11:47 PM, Mayowa Akinyemi wrote: > Hi All, > > I'm trying out SQLAlchemy, And I cant figure out how to update an > existing record without first querying for the object. > I'm using the Flask-SQLAlchemy extension and SQLAlchmeny v0.7.3 with a > Firebird 2.5.1 database > > /* my model */ > > class Supplier(db.Model): > __tablename__ = "supplier" > id = db.Column( > db.BigInteger, db.Sequence('gen_supplier_id'), > primary_key=True, nullable=False > ) > name = db.Column(db.String(100), nullable=False) > contact_person= db.Column(db.String(50), nullable=False) > phone= db.Column(db.String(45), nullable=False) > > How do i run the equivalent of 'update supplier set name = "mayowa" > where id=2' using sqla? > > From what I've read on the internets the prevailing wisdom is to use > the following: > session.query(Supplier).filter_by(id=2).update({"name": u"Mayowa"}) > > but that first results in a "select" query before the update > > there has to be a simple way to do this, but I just cant seem to find it. > > Thank you > > > Mayowa > /Aut viam inveniam aut faciam .../ > -- Jesse Panganiban Twitter: @jpanganiban GoogleTalk/Mail: me@jpanganiban.com
Hi,
On 2011-10-18 5:59 PM, Jesse Panganiban wrote:
> db.session.add(supplier)
Unnecessary for modifications. The object is automatically part of the
session if it was retrieved via Object.query.
Regards,
Armin
Thank you all for you help Regards, Mayowa *Aut viam inveniam aut faciam ...* On Tue, Oct 18, 2011 at 6:29 PM, Armin Ronacher <armin.ronacher@active-4.com > wrote: > Hi, > > On 2011-10-18 5:59 PM, Jesse Panganiban wrote: > > db.session.add(supplier) > Unnecessary for modifications. The object is automatically part of the > session if it was retrieved via Object.query. > > > Regards, > Armin > >
I didn't know that. Hehe. I stand corrected. :D On Tue, Oct 18, 2011 at 6:29 PM, Armin Ronacher <armin.ronacher@active-4.com <mailto:armin.ronacher@active-4.com>> wrote: > > Hi, > > On 2011-10-18 5:59 PM, Jesse Panganiban wrote: > > db.session.add(supplier) > Unnecessary for modifications. The object is automatically part > of the > session if it was retrieved via Object.query. > > > Regards, > Armin > > -- Jesse Panganiban Twitter: @jpanganiban GoogleTalk/Mail: me@jpanganiban.com
Hi Jesse, That's the thing I dont want to select the item first, what i'd like to do is something like this: Supplier.update(id=2).set(name="mayowa") which would result in "update supplier set name='mayowa' where id=2" without the need for a select query which is what "Supplier.query.filter_by(id=[ id here]).first()" would resullt in Thank you Mayowa *Aut viam inveniam aut faciam ...* On Tue, Oct 18, 2011 at 4:59 PM, Jesse Panganiban <me@jpanganiban.com>wrote: > Hi! > > Select the object model you are trying to update (I'll assume that you are > using the flask-sqlalchemy extension): > > Select the item you are trying to update and store it in a variable: > > supplier = Supplier.query.filter_by(id=[id here]).first() > > Like any ordinary object in python, you can always access an orm object's > properties (classvar.classproperty) and modify its values > (classvar.classproperty = newvalue). > > supplier.name = "mayowa" > > The commiting the changes > > db.session.add(supplier) > db.session.commit() > > Here's a really good documentation on how to use the sqlalchemy with flask: > http://packages.python.org/Flask-SQLAlchemy/index.html > > Hope this helped. > > > On Tuesday, 18 October, 2011 11:47 PM, Mayowa Akinyemi wrote: > > Hi All, > > I'm trying out SQLAlchemy, And I cant figure out how to update an existing > record without first querying for the object. > I'm using the Flask-SQLAlchemy extension and SQLAlchmeny v0.7.3 with a > Firebird 2.5.1 database > > /* my model */ > > class Supplier(db.Model): > __tablename__ = "supplier" > id = db.Column( > db.BigInteger, db.Sequence('gen_supplier_id'), primary_key=True, > nullable=False > ) > name = db.Column(db.String(100), nullable=False) > contact_person= db.Column(db.String(50), nullable=False) > phone= db.Column(db.String(45), nullable=False) > > How do i run the equivalent of 'update supplier set name = "mayowa" where > id=2' using sqla? > > From what I've read on the internets the prevailing wisdom is to use the > following: > session.query(Supplier).filter_by(id=2).update({"name": u"Mayowa"}) > > but that first results in a "select" query before the update > > there has to be a simple way to do this, but I just cant seem to find it. > > Thank you > > > Mayowa > *Aut viam inveniam aut faciam ...* > > > > -- > Jesse Panganiban > Twitter: @jpanganiban > GoogleTalk/Mail: me@jpanganiban.com > >
Supplier.query.filter(<your stuff here, or user filter_by, or whatever is in your where clause>).update(values) See... http://www.sqlalchemy.org/docs/orm/query.html#sqlalchemy.orm.query.Query.update -- Thadeus On Tue, Oct 18, 2011 at 11:11 AM, Mayowa Akinyemi <mayowa@gmail.com> wrote: > Hi Jesse, > That's the thing I dont want to select the item first, what i'd like to do > is something like this: > Supplier.update(id=2).set(name="mayowa") > > which would result in "update supplier set name='mayowa' where id=2" > without the need for a select query which is what > "Supplier.query.filter_by(id=[ > id here]).first()" would resullt in > > Thank you > > Mayowa > *Aut viam inveniam aut faciam ...* > > > > On Tue, Oct 18, 2011 at 4:59 PM, Jesse Panganiban <me@jpanganiban.com>wrote: > >> Hi! >> >> Select the object model you are trying to update (I'll assume that you are >> using the flask-sqlalchemy extension): >> >> Select the item you are trying to update and store it in a variable: >> >> supplier = Supplier.query.filter_by(id=[id here]).first() >> >> Like any ordinary object in python, you can always access an orm object's >> properties (classvar.classproperty) and modify its values >> (classvar.classproperty = newvalue). >> >> supplier.name = "mayowa" >> >> The commiting the changes >> >> db.session.add(supplier) >> db.session.commit() >> >> Here's a really good documentation on how to use the sqlalchemy with >> flask: http://packages.python.org/Flask-SQLAlchemy/index.html >> >> Hope this helped. >> >> >> On Tuesday, 18 October, 2011 11:47 PM, Mayowa Akinyemi wrote: >> >> Hi All, >> >> I'm trying out SQLAlchemy, And I cant figure out how to update an existing >> record without first querying for the object. >> I'm using the Flask-SQLAlchemy extension and SQLAlchmeny v0.7.3 with a >> Firebird 2.5.1 database >> >> /* my model */ >> >> class Supplier(db.Model): >> __tablename__ = "supplier" >> id = db.Column( >> db.BigInteger, db.Sequence('gen_supplier_id'), primary_key=True, >> nullable=False >> ) >> name = db.Column(db.String(100), nullable=False) >> contact_person= db.Column(db.String(50), nullable=False) >> phone= db.Column(db.String(45), nullable=False) >> >> How do i run the equivalent of 'update supplier set name = "mayowa" where >> id=2' using sqla? >> >> From what I've read on the internets the prevailing wisdom is to use the >> following: >> session.query(Supplier).filter_by(id=2).update({"name": u"Mayowa"}) >> >> but that first results in a "select" query before the update >> >> there has to be a simple way to do this, but I just cant seem to find it. >> >> Thank you >> >> >> Mayowa >> *Aut viam inveniam aut faciam ...* >> >> >> >> -- >> Jesse Panganiban >> Twitter: @jpanganiban >> GoogleTalk/Mail: me@jpanganiban.com >> >> >