python - SQLAlchemy bug? I'm not sure -
from sqlalchemy import create_engine sqlalchemy import table, column, integer, string, metadata, foreignkey sqlalchemy.orm import mapper, sessionmaker sqlalchemy.ext.declarative import declarative_base bottle import route, post, get, request import os base = declarative_base() class user(base): """table login details""" __tablename__ = 'login_table' id = column(integer, primary_key=true) username = column(string) password = column(string) email = column(string) def __init__(self, username, password, email): """create object containing info of user""" self.username = username self.password = password self.email = email def __str__(self): """return string representation""" return self.username def __repr__(self): """return object representation""" return "<user(%s)>" % self.username def force_engine(): engine = create_engine('sqlite:///users.db', echo=true) base.metadata.create_all(engine) session = sessionmaker() session.configure(bind=engine) session = session() session.commit() def spawn_session(): engine = create_engine('sqlite:///users.db') session = sessionmaker() session.configure(bind=engine) return session() def create_user(username, password, email): session = spawn_session() session.add(user(username, password, email)) session.commit() return true def register_test(): create_user('dave','test', 'a') create_user('cat','123','a') if not os.path.exists('users.db'): force_engine() @get('/register') def register_form(): return '''<form method="post"> <p>welcome new user. enter username, password, email.</p></br> <p>username: </p><input name="username" type="text" /></br> <p>password: </p><input name="password" type="password" /></br> <p>email: </p><input name="email" type="text" /></br> <input type="submit" value="submit" /> </from>''' @post('/register') def register_submit(): name = request.forms.get('username') password = request.forms.get('password') email = request.forms.get('email') authd = create_user(name, password, email) if authd: return "<p>user created</p>" else: return "<p>failure</p>"
this code i'm using. don't tell me security, or else. me figure out why causing traceback
>>> register_test() traceback (most recent call last): file "<interactive input>", line 1, in <module> file "c:\users\public\documents\webapps\login\register.py", line 54, in register_test create_user('dave','test', 'a') file "c:\users\public\documents\webapps\login\register.py", line 50, in create_user session.commit() file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\orm\session.py", line 623, in commit self.transaction.commit() file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\orm\session.py", line 385, in commit self._prepare_impl() file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\orm\session.py", line 369, in _prepare_impl self.session.flush() file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\orm\session.py", line 1397, in flush self._flush(objects) file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\orm\session.py", line 1478, in _flush flush_context.execute() file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\orm\unitofwork.py", line 304, in execute rec.execute(self) file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\orm\unitofwork.py", line 448, in execute uow file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\orm\mapper.py", line 1872, in _save_obj execute(statement, params) file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\engine\base.py", line 1191, in execute params) file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\engine\base.py", line 1271, in _execute_clauseelement return self.__execute_context(context) file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\engine\base.py", line 1302, in __execute_context context.parameters[0], context=context) file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\engine\base.py", line 1401, in _cursor_execute context) file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\engine\base.py", line 1394, in _cursor_execute context) file "c:\python26\lib\site-packages\sqlalchemy-0.6.5-py2.6.egg\sqlalchemy\engine\default.py", line 299, in do_execute cursor.execute(statement, parameters) operationalerror: (operationalerror) table login_table has no column named email u'insert login_table (username, password, email) values (?, ?, ?)' ('dave', 'test', 'a')
even when close down python entirely, , delete database entirely still same execption
very nice write here using sqlalchemy , sqlite in tutorial
you need verify on @ least version 0.7 of sqlalchemy
>>> import sqlalchemy >>> sqlalchemy.__version__ 0.7.0
use echo display sql generated
>>> sqlalchemy import create_engine >>> engine = create_engine('sqlite:///users.db', echo=true)
and might have missed important step
>>> sqlalchemy.ext.declarative import declarative_base >>> base = declarative_base()
finally verify have created table correctly
>>> user.__table__ table('users', metadata(none), column('id', integer(), table=<users>, primary_key=true, nullable=false), column('name', string(), table=<users>), column('username', string(), table=<users>), column('password', string(), table=<users>), column('email', string(), table=<users>), schema=none)
Comments
Post a Comment