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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.