Trying to walk through tutorial, getting (OperationalError) no such table

3,643 views
Skip to first unread message

MichelleB

unread,
Sep 15, 2010, 3:52:55 PM9/15/10
to sqlalchemy
This is driving me crazy since I can't even get through the tutorial :
( Thanks for any help! code, then error below.

#! /usr/local/bin/python

""" dbtestpy

Walking through sqlalchemy tutorial

Author: Michelle Brenner
"""

from sqlalchemy import Table, Column, Integer, Float, Sequence,
String, MetaData, ForeignKey, create_engine, or_, func
from sqlalchemy.orm import mapper, sessionmaker
from sqlalchemy.orm.exc import NoResultFound
engine = create_engine('sqlite:///dbtest.db', echo=False)


Session = sessionmaker(bind=engine)
Session.configure(bind=engine)
session = Session()

#creating db
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base(bind=engine)
metadata = Base.metadata
metadata.create_all(engine)

class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
password = Column(String)
def __init__(self, name, fullname, password):
self.name = name
self.fullname = fullname
self.password = password
def __repr__(self):
return "<User('%s','%s', '%s')>" % (self.name, self.fullname,
self.password)

users_table = User.__table__

#adding data
ed_user = User('ed', 'Ed Jones', 'edspassword')

#querying data
session.add(ed_user)
session.commit()

#our_user = session.query(User).filter_by(name='ed').first()
#our_user

Traceback (most recent call last):
File "dbtest.py", line 48, in ?
session.commit()
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/orm/
session.py", line 673, in commit
self.transaction.commit()
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/orm/
session.py", line 378, in commit
self._prepare_impl()
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/orm/
session.py", line 362, in _prepare_impl
self.session.flush()
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/orm/
session.py", line 1354, in flush
self._flush(objects)
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/orm/
session.py", line 1432, in _flush
flush_context.execute()
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/orm/
unitofwork.py", line 257, in execute
UOWExecutor().execute(self, tasks)
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/orm/
unitofwork.py", line 720, in execute
self.execute_save_steps(trans, task)
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/orm/
unitofwork.py", line 735, in execute_save_steps
self.save_objects(trans, task)
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/orm/
unitofwork.py", line 726, in save_objects
task.mapper._save_obj(task.polymorphic_tosave_objects, trans)
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/orm/
mapper.py", line 1387, in _save_obj
c = connection.execute(statement.values(value_params), params)
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/engine/
base.py", line 824, in execute
return Connection.executors[c](self, object, multiparams, params)
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/engine/
base.py", line 874, in _execute_clauseelement
return self.__execute_context(context)
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/engine/
base.py", line 896, in __execute_context
self._cursor_execute(context.cursor, context.statement,
context.parameters[0], context=context)
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/engine/
base.py", line 950, in _cursor_execute
self._handle_dbapi_exception(e, statement, parameters, cursor,
context)
File "/net/vol240/shots/spi/home/python/common/sqlalchemy/engine/
base.py", line 931, in _handle_dbapi_exception
raise exc.DBAPIError.instance(statement, parameters, e,
connection_invalidated=is_disconnect)
sqlalchemy.exc.OperationalError: (OperationalError) no such table:
users u'INSERT INTO users (name, fullname, password) VALUES
(?, ?, ?)' ['ed', 'Ed Jones', 'edspassword']

Roger Demetrescu

unread,
Sep 15, 2010, 4:04:01 PM9/15/10
to sqlal...@googlegroups.com
Try moving the "metadata.create_all(engine)" below the User class declaration.

Like:


<snipped code>

class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
password = Column(String)
def __init__(self, name, fullname, password):
self.name = name
self.fullname = fullname
self.password = password
def __repr__(self):
return "<User('%s','%s', '%s')>" % (self.name, self.fullname,
self.password)

metadata.create_all(engine)


<snipped code>


After User class is evaluated, the metadata object will have knowledge
about the "users" table.


Cheers

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

Reply all
Reply to author
Forward
0 new messages