You're best starting with the declarative usage patterns described in the ORM
tutorial at http://www.sqlalchemy.org/docs/orm/tutorial.html, starting with
http://www.sqlalchemy.org/docs/orm/tutorial.html#creating-table-class-and-mapper-all-at-once-declaratively.
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 [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.
>
--
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.