Works for me (once I add metadata.create_all(bind=engine) ) ... possibly you
have an old SQLite that doesn't do the auto-incrementing primary key thing?
- G.
On Wed, Feb 18, 2009 at 2:26 PM, Marcin Krol <[email protected]> wrote:
>
> Hello,
>
> I just started learning sqlalchemy, my version is 0.5.2, I'm reading the
> tutorial (Object Relational Tutorial) and produced this code:
>
>
> from sqlalchemy import create_engine, Table, Column, Integer, String,
> MetaData, ForeignKey
> from sqlalchemy.orm import mapper, sessionmaker
>
> engine = create_engine('sqlite:////test.sqlite',echo=True)
>
> metadata = MetaData()
>
> users_table = Table('users', metadata,
> Column('id',Integer,primary_key=True),
> Column('name',String),
> Column('fullname',String),
> Column('password',String))
>
> class User(object):
> def __init__(self,name,fullname,password):
> self.name = name
> self.fullname = fullname
> self.password = password
>
>
>
> mapper(User, users_table)
>
> ed_user = User('ed','Ed Jones','sdffa')
>
> Session=sessionmaker(bind=engine)
> session=Session()
>
> session.add(ed_user)
> session.commit()
>
> our_user=session.query(User).filter_by(name='ed').first()
>
> res = ed_user is our_user
> print res
>
>
> However, it fails due to exception:
>
> Traceback (most recent call last):
> File "C:/Python26/userssqa.py", line 31, in <module>
> session.commit()
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\session.py",
> line 673, in commit
> self.transaction.commit()
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\session.py",
> line 378, in commit
> self._prepare_impl()
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\session.py",
> line 362, in _prepare_impl
> self.session.flush()
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\session.py",
> line 1347, in flush
> self._flush(objects)
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\session.py",
> line 1417, in _flush
> flush_context.execute()
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\unitofwork.py",
> line 244, in execute
> UOWExecutor().execute(self, tasks)
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\unitofwork.py",
> line 707, in execute
> self.execute_save_steps(trans, task)
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\unitofwork.py",
> line 722, in execute_save_steps
> self.save_objects(trans, task)
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\unitofwork.py",
> line 713, in save_objects
> task.mapper._save_obj(task.polymorphic_tosave_objects, trans)
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\orm\mapper.py",
> line 1352, in _save_obj
> c = connection.execute(statement.values(value_params), params)
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\engine\base.py",
> line 824, in execute
> return Connection.executors[c](self, object, multiparams, params)
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\engine\base.py",
> line 874, in _execute_clauseelement
> return self.__execute_context(context)
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\engine\base.py",
> line 896, in __execute_context
> self._cursor_execute(context.cursor, context.statement,
> context.parameters[0], context=context)
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\engine\base.py",
> line 950, in _cursor_execute
> self._handle_dbapi_exception(e, statement, parameters, cursor, context)
> File
>
> "c:\python26\lib\site-packages\sqlalchemy-0.5.2-py2.6.egg\sqlalchemy\engine\base.py",
> line 931, in _handle_dbapi_exception
> raise exc.DBAPIError.instance(statement, parameters, e,
> connection_invalidated=is_disconnect)
> IntegrityError: (IntegrityError) users.id may not be NULL u'INSERT INTO
> users (name, fullname, password) VALUES (?, ?, ?)' ['ed', 'Ed Jones',
> 'sdffa']
>
> This is pretty obvious, 'id' integer column has not been filled. But I
> have no idea how to remedy this, since this seems to be dependent on
> smth in the guts of sqlalchemy. Help!
>
> Regards,
> mk
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---