I'm using the mapper and Table classes from SQLAlchemy 0.2.8 to build
a simple user registration table and mapping. Im using Postgresql 8.1
as the RDBMS and am using TurboGears 1.0b1 to work with SQLalchemy.

I'm getting an exception when I try to update a row, even though my
code is very similar to the tutorial code. I'll step through what I'm
doing below, any help would be appreciated.

I can manage to insert a new record like this:

>>> session = create_session()
>>> r = Registration()
>>> r.id = 1
>>> r.email_address = '[EMAIL PROTECTED]'
>>> r.guid = '123'
>>> session.save(r)
>>> session.flush()
[2006-10-02 22:45:16,320] [engine]: INSERT INTO registrations (id,
email_address, guid, row_version) VALUES (%(id)s, %(email_address)s,
%(guid)s, %(row_version)s)
[2006-10-02 22:45:16,320] [engine]: {'row_version': 0, 'guid': '123',
'email_address': '[EMAIL PROTECTED]', 'id': 1}


so far so good. The row is in the database. I retrieve the row, and
try and modify it.


>>> session.clear()
>>> r = session.query(Registration).get_by(id=1)
[2006-10-02 22:46:54,951] [engine]: SELECT registrations.row_version
AS registrations_row_version, registrations.guid AS
registrations_guid, registrations.email_address AS
registrations_email_address, registrations.id AS registrations_id
FROM registrations
WHERE registrations.id = %(registrations_id)s ORDER BY registrations.id
 LIMIT 1
[2006-10-02 22:46:54,951] [engine]: {'registrations_id': 1}
>>> r
<webapp.model.Registration object at 0xb739a42c>
>>> r.email_address
u'[EMAIL PROTECTED]'
>>> r.email_address = '[EMAIL PROTECTED]'
>>> r in session
True
>>> session.flush()
Traceback (most recent call last):
  File "<console>", line 1, in ?
  File "build/bdist.linux-i686/egg/sqlalchemy/orm/session.py", line
234, in flush
  File "build/bdist.linux-i686/egg/sqlalchemy/orm/unitofwork.py", line
207, in flush
  File "build/bdist.linux-i686/egg/sqlalchemy/orm/unitofwork.py", line
377, in execute
  File "build/bdist.linux-i686/egg/sqlalchemy/orm/unitofwork.py", line
645, in execute
  File "build/bdist.linux-i686/egg/sqlalchemy/orm/unitofwork.py", line
599, in _save_objects
  File "build/bdist.linux-i686/egg/sqlalchemy/orm/mapper.py", line
821, in save_obj
AttributeError: 'str' object has no attribute '_label'
>>>

Eek. I wasn't expecting an exception here. The code is very similar to
the tutorial code, so I can't see where I'm going wrong. Am I doing
something wrong? Or is this a bug from somewhere else?

If it helps, here is the code I'm using to build my table and mapping objects.


def build_mapper(cls, use_optimistic_concurrency=True):
    version_col = None
    if use_optimistic_concurrency:
        version_col = 'row_version'
    assert hasattr(cls.meta,'table')
    cls.meta.mapper = mapper(cls, cls.meta.table, version_id_col=version_col)


class Registration(object):
    class meta:
        table = Table('registrations', metadata,
            Column('id', Integer, primary_key=True),
            Column('email_address', Unicode(1024), nullable=False),
            Column('guid', String(32)),
            Column('row_version', Integer, nullable=False, default=0))

build_mapper(Registration)

-Sw.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to