[sqlalchemy] updates using declarative extention

2011-06-15 Thread Liju
I'm trying to update one table. I'm using declarative extention for
all db operations. I have successfully implemented updates using
session after I first query the object and session keeps track of the
changes subsequently. but I think for what I do, that prior query is a
waste but prefer a straight update. Is this possible using declarative
ext ?

Thx.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: updates using declarative extention

2011-06-15 Thread Liju
Cool that worked. This is what I have done.

result =
session.query(Purchaser).filter(Purchaser.id==purchaser.id).update({'name':purchaser.name,'status':purchaser.status})

Thx Mike.


On Jun 15, 6:21 pm, Mike Conley mconl...@gmail.com wrote:
 seehttp://www.sqlalchemy.org/docs/orm/query.html#sqlalchemy.orm.query.Qu...

 We use session.query(..).update(..,synchronize_session=False) for
 updates where we are sure about the criteria and session state. This
 generates an update statement without any selects.

 --
 Mike Conley

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] mapper error, and db object cannot be incremented

2011-06-14 Thread Liju
I'm new to SQLAlchemy. I wrote a method that retrieves a record,
update the object after incrementing it by 1, and return that record
object to the caller (pyramid view). Following is the test function. I
get following errors :

1) when I call this method multiple times, I get an error that say
ArgumentError: Class 'class 'cas.models.Models'' already has a
primary mapper defined. Use non_primary=True to create a non primary
Mapper. clear_mappers() will remove *all* current mappers from all
classes.

As a resolution i called 'clear_mappers()' before invoking mapper.

2) I cant seem to increment the attribute of an object in orm session.
My understanding is that once a record is retrieved in an ORM session,
Session object keeps track of any changes to the record object and
updates the record when session.flush() is invoked.
But I get error TypeError: unsupported operand type(s) for +:
'instancemethod' and 'int'

Can someone please explain to me what I'm doing wrong ?

class Models(object):pass

def countAndIncrement():
metadata = MetaData('sqlite:///CAS.db')

model_table = Table('models',
metadata,
Column('id',Integer,primary_key=True),
Column('name',String(40)),
Column('value',Integer)
)

clear_mappers()

mapper(Models,model_table)   #  already a
primary mapper defined error (when I call this function multiple times

Session = sessionmaker()
session = Session()

model = session.query(Models).filter(Models.id==1)

model.value = model.value + 1 #
 increment error

session.flush()
session.close()

return model

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: mapper error, and db object cannot be incremented

2011-06-14 Thread Liju
Thank you so much Michael. By the way SQLAlchemy and Mako template are
so cool. Thank you so much for all your efforts.

Liju.

On Jun 14, 9:38 am, Michael Bayer mike...@zzzcomputing.com wrote:
 You're best starting with the declarative usage patterns described in the ORM 
 tutorial athttp://www.sqlalchemy.org/docs/orm/tutorial.html, starting 
 withhttp://www.sqlalchemy.org/docs/orm/tutorial.html#creating-table-class 
  I would declare the class + table + mapping at once, to eliminate any 
 confusion regarding mapping, which is not a per-usage operation; it is a 
 permanent operation applied to a model class only once.     The mapper() + 
 Table pattern is not as easy to use and it's being de-emphasized in the 
 documentation.

 The second error implies your class has a method called value() on it which 
 is conflicting with the mapped attribute of .value.

 On Jun 14, 2011, at 12:22 PM, Liju wrote:







  I'm new to SQLAlchemy. I wrote a method that retrieves a record,
  update the object after incrementing it by 1, and return that record
  object to the caller (pyramid view). Following is the test function. I
  get following errors :

  1) when I call this method multiple times, I get an error that say
  ArgumentError: Class 'class 'cas.models.Models'' already has a
  primary mapper defined. Use non_primary=True to create a non primary
  Mapper. clear_mappers() will remove *all* current mappers from all
  classes.

  As a resolution i called 'clear_mappers()' before invoking mapper.

  2) I cant seem to increment the attribute of an object in orm session.
  My understanding is that once a record is retrieved in an ORM session,
  Session object keeps track of any changes to the record object and
  updates the record when session.flush() is invoked.
  But I get error TypeError: unsupported operand type(s) for +:
  'instancemethod' and 'int'

  Can someone please explain to me what I'm doing wrong ?

  class Models(object):pass

  def countAndIncrement():
     metadata = MetaData('sqlite:///CAS.db')

     model_table = Table('models',
                         metadata,
                         Column('id',Integer,primary_key=True),
                         Column('name',String(40)),
                         Column('value',Integer)
                         )

     clear_mappers()

     mapper(Models,model_table)               #  already a
  primary mapper defined error (when I call this function multiple times

     Session = sessionmaker()
     session = Session()

     model = session.query(Models).filter(Models.id==1)

     model.value = model.value + 1                                 #
   increment error

     session.flush()
     session.close()

     return model

  --
  You received this message because you are subscribed to the Google Groups 
  sqlalchemy group.
  To post to this group, send email to sqlalchemy@googlegroups.com.
  To unsubscribe from this group, send email to 
  sqlalchemy+unsubscr...@googlegroups.com.
  For more options, visit this group 
  athttp://groups.google.com/group/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] SQLAlchemy best practise

2011-06-06 Thread Liju
I'm new to SQLAlchemy and loving it. But reading all documentation
online makes me wonder if there are any best practice documentation
for sqlalchemy out there ?

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.