[sqlalchemy] Re: Session will not commit within a class
UGGG! I have been fighting with this for a while. I never thought to just use an actual file for my db instead of using memory. I was using memory because I was only wanting to test things and didn't want to keep the data. Seems that perhaps due to cherrypy the database was being deleted. I later moved the create_all() statement into the index sub and found that it worked. Following that when I loaded a lot of info into the db and browsed to an outside website and back all the data was gone. My original code now works, all I had to do it use a db file instead of a db in memory. I'm glad I found a solution. But the number of times I read through sqlalchemy docs to find my problem just to realize there really wasn't anything wrong all along is very frustrating lol. Thanks for the help. Again I am new to SQLAlchemy so if you know any best practices or have any suggestions in general for web development in python, please shoot them my way. V/R Kevin --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Session will not commit within a class
On Wed, Aug 5, 2009 at 08:16, kportertxkporte...@gmail.com wrote: O on a side note would you recommend elixir over declarative_base? The biggest difference between the two is that Elixir can generate some columns and tables for you (using common patterns). Michael Bayer probably won't recommend Elixir since he wrote declarative and he doesn't like much the generate stuff approach taken by Elixir. Whether you like/want that feature or not should decide whether you go with Elixir or declarative. -- Gaëtan de Menten http://openhex.org --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Session will not commit within a class
On Wed, Aug 5, 2009 at 08:13, kportertxkporte...@gmail.com wrote: UGGG! I have been fighting with this for a while. I never thought to just use an actual file for my db instead of using memory. I was using memory because I was only wanting to test things and didn't want to keep the data. Seems that perhaps due to cherrypy the database was being deleted. I later moved the create_all() statement into the index sub and found that it worked. Following that when I loaded a lot of info into the db and browsed to an outside website and back all the data was gone. In case you were wondering, the reason for all your troubles is simple: cherrypy is multi-threaded. SQLite in-memory databases are thread-local. If you call create_all in all your threads, you create tables in all the different databases created by each thread. When you later revisited your site, cherrypy served you the page through another thread, hence another database, hence you saw no data. -- Gaëtan de Menten http://openhex.org --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Session will not commit within a class
this appears to be an Elixir issue, check on their list. kportertx wrote: for some reason I cannot get my session to commit within a class Here is the error. Traceback (most recent call last): File /usr/local/lib/python2.6/dist-packages/cherrypy/ _cprequest.py, line 606, in respond cherrypy.response.body = self.handler() File /usr/local/lib/python2.6/dist-packages/cherrypy/ _cpdispatch.py, line 25, in __call__ return self.callable(*self.args, **self.kwargs) File WebTest.py, line 106, in index session.flush() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ scoping.py, line 123, in do return getattr(self.registry(), name)(*args, **kwargs) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 1354, in flush self._flush(objects) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 1432, in _flush flush_context.execute() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 257, in execute UOWExecutor().execute(self, tasks) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 720, in execute self.execute_save_steps(trans, task) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 735, in execute_save_steps self.save_objects(trans, task) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 726, in save_objects task.mapper._save_obj(task.polymorphic_tosave_objects, trans) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ mapper.py, line 1387, in _save_obj c = connection.execute(statement.values(value_params), params) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 824, in execute return Connection.executors[c](self, object, multiparams, params) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 874, in _execute_clauseelement return self.__execute_context(context) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 896, in __execute_context self._cursor_execute(context.cursor, context.statement, context.parameters[0], context=context) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 950, in _cursor_execute self._handle_dbapi_exception(e, statement, parameters, cursor, context) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 931, in _handle_dbapi_exception raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect) OperationalError: (OperationalError) no such table: __main___test u'INSERT INTO __main___test (name) VALUES (?)' ['network'] And here is the code #!/usr/bin/env python # # # MODEL # # # from elixir import * global session, metadata metadata.bind = 'sqlite:///:memory:' metadata.echo = True class Test(Entity): ORM for Tests. id = Field(Integer, primary_key = True) name = Field(Unicode(50), required = True) questions = OneToMany('Question') def __init__(self,name): Accepts a name for a test #TODO: Ensure valid input self.name = name class Question(Entity): ORM for Questions. id = Field(Integer, primary_key = True) question = Field(UnicodeText, required = True) test = ManyToOne('Test') answers = OneToMany('Answer') def __init__(self,test_id, question): Accepts a test_id and a question to sign that test #TODO: Ensure valid input self.question = question class Answer(Entity): ORM for Answers. id = Field(Integer, primary_key = True) answer = Field(UnicodeText, required = True) correct = Field(Unicode(1), required = True) # y for correct n for not question = ManyToOne('Question') def __init__(self,question_id,answer,correct=0): Accepts a question_id and a answer to assign to that question. Optionally it accepts correct, y for correct, n for not Default for correct is n #TODO: Ensure vaild input self.answer = answer self.correct = correct setup_all() create_all() # # # VIEW/CONTROLLER # #Will Seperate Later # # # import cherrypy class WebTest(object): Cherrypy server root def header(self): Defines a header to be used for web pages... Should be temporary return 'htmlheadtitlepyWebTest/title/headbody' def footer(self): Defines a footer to be used for
[sqlalchemy] Re: Session will not commit within a class
I moved to elixir when declarative_base caused the exact same issue. Elixir still uses sqlalchemy's session so am I at least using the session correctly? Here is declarative_base code #!/usr/bin/env python # # # MODEL # # # import sqlalchemy from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey from sqlalchemy.orm import relation, backref from sqlalchemy.orm import mapper from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import mapper # engine is the connection to the database engine = create_engine('sqlite:///:memory:', echo=True) # TODO: Move to a Conf file Base = declarative_base(bind=engine) class Test(Base): ORM for Tests. __tablename__ = tests id = Column(Integer, primary_key=True) name = Column(String(50)) def __init__(self,name): Accepts a name for a test #TODO: Ensure valid input self.name = name class Question(Base): ORM for Questions. __tablename__ = questions id = Column(Integer, primary_key=True) test_id = Column(Integer, ForeignKey(tests.id)) question = Column(String) test = relation(Test, backref=backref('questions', order_by=id)) def __init__(self,test_id, question): Accepts a test_id and a question to sign that test #TODO: Ensure valid input self.question = question self.test_id = test_id class Answer(Base): ORM for Answers. __tablename__ = answers id = Column(Integer, primary_key=True) question_id = Column(Integer, ForeignKey(questions.id)) answer = Column(String) correct = Column(String(1), default='n') # y for correct n for not question = relation(Question, backref=backref('answers', order_by=id)) def __init__(self,question_id,answer,correct=0): Accepts a question_id and a answer to assign to that question. Optionally it accepts correct, y for correct, n for not Default for correct is n #TODO: Ensure vaild input self.question_id = question_id self.answer = answer self.correct = correct Base.metadata.create_all(engine) # Create all tables # Session class is how we talk to the engine (database) Session = sessionmaker(bind=engine) # # # VIEW/CONTROLLER # #Will Seperate Later # # # import cherrypy class WebTest(object): Cherrypy server root def header(self): Defines a header to be used for web pages... Should be temporary return htmlheadtitlepyWebTest/title/headbody def footer(self): Defines a footer to be used for web pages... Should also be temporary return /body/html @cherrypy.expose() def index(self): Root page for pyWebTest server session = Session() test = Test(network+) session.add(test) print session.dirty session.commit print session.dirty return self.header() + h1Web Test/h1 + self.footer() def main(): cherrypy.quickstart(WebTest()) return 0 if __name__ == '__main__': main() On Aug 4, 7:49 am, Michael Bayer mike...@zzzcomputing.com wrote: this appears to be an Elixir issue, check on their list. kportertx wrote: for some reason I cannot get my session to commit within a class Here is the error. Traceback (most recent call last): File /usr/local/lib/python2.6/dist-packages/cherrypy/ _cprequest.py, line 606, in respond cherrypy.response.body = self.handler() File /usr/local/lib/python2.6/dist-packages/cherrypy/ _cpdispatch.py, line 25, in __call__ return self.callable(*self.args, **self.kwargs) File WebTest.py, line 106, in index session.flush() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ scoping.py, line 123, in do return getattr(self.registry(), name)(*args, **kwargs) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 1354, in flush self._flush(objects) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 1432, in _flush flush_context.execute() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 257, in execute UOWExecutor().execute(self, tasks) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 720, in execute self.execute_save_steps(trans, task) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 735, in execute_save_steps self.save_objects(trans, task) File
[sqlalchemy] Re: Session will not commit within a class
kportertx wrote: I moved to elixir when declarative_base caused the exact same issue. Elixir still uses sqlalchemy's session so am I at least using the session correctly? thats unlikely. the error claims it is looking for a nonexistent table called __main__test, which is the kind of name Elixir generates (SQLalchemy does not generate names). Your code example below does not include a table of this name. in Elixir, the error indicates a Table is being generated after metadata.create_all() occurs. this issue wouldn't occur in SQLAlchemy unless you didn't define your full model when you created all the tables. make sure all your classes are imported when you call create_all(). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Session will not commit within a class
@ Michael Bayer, Sorry forgot to post the new error from the code without elixir, also there was a problem with the session.commit statement in that last post. It was supposed to be session.commit() not session.commit. Traceback (most recent call last): File /usr/local/lib/python2.6/dist-packages/cherrypy/ _cprequest.py, line 606, in respond cherrypy.response.body = self.handler() File /usr/local/lib/python2.6/dist-packages/cherrypy/ _cpdispatch.py, line 25, in __call__ return self.callable(*self.args, **self.kwargs) File WebTest.py, line 124, in index session.commit() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 673, in commit self.transaction.commit() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 378, in commit self._prepare_impl() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 362, in _prepare_impl self.session.flush() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 1354, in flush self._flush(objects) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 1432, in _flush flush_context.execute() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 257, in execute UOWExecutor().execute(self, tasks) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 720, in execute self.execute_save_steps(trans, task) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 735, in execute_save_steps self.save_objects(trans, task) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 726, in save_objects task.mapper._save_obj(task.polymorphic_tosave_objects, trans) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ mapper.py, line 1387, in _save_obj c = connection.execute(statement.values(value_params), params) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 824, in execute return Connection.executors[c](self, object, multiparams, params) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 874, in _execute_clauseelement return self.__execute_context(context) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 896, in __execute_context self._cursor_execute(context.cursor, context.statement, context.parameters[0], context=context) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 950, in _cursor_execute self._handle_dbapi_exception(e, statement, parameters, cursor, context) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 931, in _handle_dbapi_exception raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect) OperationalError: (OperationalError) no such table: tests u'INSERT INTO tests (name) VALUES (?)' ['network+'] Here is the corrected code Traceback (most recent call last): File /usr/local/lib/python2.6/dist-packages/cherrypy/ _cprequest.py, line 606, in respond cherrypy.response.body = self.handler() File /usr/local/lib/python2.6/dist-packages/cherrypy/ _cpdispatch.py, line 25, in __call__ return self.callable(*self.args, **self.kwargs) File WebTest.py, line 124, in index session.commit() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 673, in commit self.transaction.commit() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 378, in commit self._prepare_impl() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 362, in _prepare_impl self.session.flush() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 1354, in flush self._flush(objects) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 1432, in _flush flush_context.execute() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 257, in execute UOWExecutor().execute(self, tasks) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 720, in execute self.execute_save_steps(trans, task) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 735, in execute_save_steps self.save_objects(trans, task) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 726, in save_objects task.mapper._save_obj(task.polymorphic_tosave_objects, trans) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ mapper.py, line 1387, in _save_obj c = connection.execute(statement.values(value_params), params) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 824, in execute return Connection.executors[c](self, object, multiparams, params) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line
[sqlalchemy] Re: Session will not commit within a class
kportertx wrote: @ Michael Bayer, Sorry forgot to post the new error from the code without elixir, also there was a problem with the session.commit statement in that last post. It was supposed to be session.commit() not session.commit. ok so, you called create_all() after all modules imported, test is created, and that will fix your problem. Traceback (most recent call last): File /usr/local/lib/python2.6/dist-packages/cherrypy/ _cprequest.py, line 606, in respond cherrypy.response.body = self.handler() File /usr/local/lib/python2.6/dist-packages/cherrypy/ _cpdispatch.py, line 25, in __call__ return self.callable(*self.args, **self.kwargs) File WebTest.py, line 124, in index session.commit() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 673, in commit self.transaction.commit() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 378, in commit self._prepare_impl() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 362, in _prepare_impl self.session.flush() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 1354, in flush self._flush(objects) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 1432, in _flush flush_context.execute() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 257, in execute UOWExecutor().execute(self, tasks) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 720, in execute self.execute_save_steps(trans, task) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 735, in execute_save_steps self.save_objects(trans, task) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 726, in save_objects task.mapper._save_obj(task.polymorphic_tosave_objects, trans) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ mapper.py, line 1387, in _save_obj c = connection.execute(statement.values(value_params), params) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 824, in execute return Connection.executors[c](self, object, multiparams, params) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 874, in _execute_clauseelement return self.__execute_context(context) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 896, in __execute_context self._cursor_execute(context.cursor, context.statement, context.parameters[0], context=context) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 950, in _cursor_execute self._handle_dbapi_exception(e, statement, parameters, cursor, context) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/engine/ base.py, line 931, in _handle_dbapi_exception raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect) OperationalError: (OperationalError) no such table: tests u'INSERT INTO tests (name) VALUES (?)' ['network+'] Here is the corrected code Traceback (most recent call last): File /usr/local/lib/python2.6/dist-packages/cherrypy/ _cprequest.py, line 606, in respond cherrypy.response.body = self.handler() File /usr/local/lib/python2.6/dist-packages/cherrypy/ _cpdispatch.py, line 25, in __call__ return self.callable(*self.args, **self.kwargs) File WebTest.py, line 124, in index session.commit() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 673, in commit self.transaction.commit() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 378, in commit self._prepare_impl() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 362, in _prepare_impl self.session.flush() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 1354, in flush self._flush(objects) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ session.py, line 1432, in _flush flush_context.execute() File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 257, in execute UOWExecutor().execute(self, tasks) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 720, in execute self.execute_save_steps(trans, task) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 735, in execute_save_steps self.save_objects(trans, task) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ unitofwork.py, line 726, in save_objects task.mapper._save_obj(task.polymorphic_tosave_objects, trans) File /usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/ mapper.py, line 1387, in _save_obj c = connection.execute(statement.values(value_params), params) File
[sqlalchemy] Re: Session will not commit within a class
But this is all in one file, create_all() after all classes defining the model are loaded. I noticed earlier I accidentally posted the error twice, here is the source code. Could you try running this and see if you run into the same problem. Again this all one file. This is also my first week with sqlalchemy so if there happens to be something that I should change feel free to tell me. I'm just trying to learn another way to ' program for the web, been using PHP for years. Generally I don't use ORM but this one caught my eye, and I thought I would give it a try. BEGIN CODE #!/usr/bin/env python # # # MODEL # # # import sqlalchemy from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey from sqlalchemy.orm import relation, backref from sqlalchemy.orm import mapper from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import mapper # engine is the connection to the database engine = create_engine('sqlite:///:memory:', echo=True) # TODO: Move to a Conf file Base = declarative_base(bind=engine) class Test(Base): ORM for Tests. __tablename__ = tests id = Column(Integer, primary_key=True) name = Column(String(50)) def __init__(self,name): Accepts a name for a test #TODO: Ensure valid input self.name = name class Question(Base): ORM for Questions. __tablename__ = questions id = Column(Integer, primary_key=True) test_id = Column(Integer, ForeignKey(tests.id)) question = Column(String) test = relation(Test, backref=backref('questions', order_by=id)) def __init__(self,test_id, question): Accepts a test_id and a question to sign that test #TODO: Ensure valid input self.question = question self.test_id = test_id class Answer(Base): ORM for Answers. __tablename__ = answers id = Column(Integer, primary_key=True) question_id = Column(Integer, ForeignKey(questions.id)) answer = Column(String) correct = Column(String(1), default='n') # y for correct n for not question = relation(Question, backref=backref('answers', order_by=id)) def __init__(self,question_id,answer,correct=0): Accepts a question_id and a answer to assign to that question. Optionally it accepts correct, y for correct, n for not Default for correct is n #TODO: Ensure vaild input self.question_id = question_id self.answer = answer self.correct = correct def setup_model(): Base.metadata.create_all(engine) # Create all tables # Session class is how we talk to the engine (database) global Session Session = sessionmaker(bind=engine) # # # VIEW/CONTROLLER # #Will Seperate Later # # # import cherrypy class WebTest(object): Cherrypy server root def __init__(self): setup_model() def header(self): Defines a header to be used for web pages... Should be temporary return htmlheadtitlepyWebTest/title/headbody def footer(self): Defines a footer to be used for web pages... Should also be temporary return /body/html @cherrypy.expose() def index(self): Root page for pyWebTest server output = 'Begin Test br /' session = Session() test = Test(network+) session.add(test) output = output + str(session.new) + 'br /' session.commit() output = output + str(session.new) + 'br /' print session.dirty return self.header() + output + self.footer() def main(): cherrypy.quickstart(WebTest()) return 0 if __name__ == '__main__': main() END CODE --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Session will not commit within a class
Could this have something to do with using a database in memory? I just noticed if I call create_all() within my class every time I want to update the database it works. Also it is able to add multiple items by pressing refresh. But when I browse away from the web page and return later it resets the database in memory. Perhaps this is a problem with cherrypy and not sqlalchemy. On Aug 4, 10:22 pm, kportertx kporte...@gmail.com wrote: But this is all in one file, create_all() after all classes defining the model are loaded. I noticed earlier I accidentally posted the error twice, here is the source code. Could you try running this and see if you run into the same problem. Again this all one file. This is also my first week with sqlalchemy so if there happens to be something that I should change feel free to tell me. I'm just trying to learn another way to ' program for the web, been using PHP for years. Generally I don't use ORM but this one caught my eye, and I thought I would give it a try. BEGIN CODE #!/usr/bin/env python # # # MODEL # # # import sqlalchemy from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey from sqlalchemy.orm import relation, backref from sqlalchemy.orm import mapper from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import mapper # engine is the connection to the database engine = create_engine('sqlite:///:memory:', echo=True) # TODO: Move to a Conf file Base = declarative_base(bind=engine) class Test(Base): ORM for Tests. __tablename__ = tests id = Column(Integer, primary_key=True) name = Column(String(50)) def __init__(self,name): Accepts a name for a test #TODO: Ensure valid input self.name = name class Question(Base): ORM for Questions. __tablename__ = questions id = Column(Integer, primary_key=True) test_id = Column(Integer, ForeignKey(tests.id)) question = Column(String) test = relation(Test, backref=backref('questions', order_by=id)) def __init__(self,test_id, question): Accepts a test_id and a question to sign that test #TODO: Ensure valid input self.question = question self.test_id = test_id class Answer(Base): ORM for Answers. __tablename__ = answers id = Column(Integer, primary_key=True) question_id = Column(Integer, ForeignKey(questions.id)) answer = Column(String) correct = Column(String(1), default='n') # y for correct n for not question = relation(Question, backref=backref('answers', order_by=id)) def __init__(self,question_id,answer,correct=0): Accepts a question_id and a answer to assign to that question. Optionally it accepts correct, y for correct, n for not Default for correct is n #TODO: Ensure vaild input self.question_id = question_id self.answer = answer self.correct = correct def setup_model(): Base.metadata.create_all(engine) # Create all tables # Session class is how we talk to the engine (database) global Session Session = sessionmaker(bind=engine) # # # VIEW/CONTROLLER # # Will Seperate Later # # # import cherrypy class WebTest(object): Cherrypy server root def __init__(self): setup_model() def header(self): Defines a header to be used for web pages... Should be temporary return htmlheadtitlepyWebTest/title/headbody def footer(self): Defines a footer to be used for web pages... Should also be temporary return /body/html @cherrypy.expose() def index(self): Root page for pyWebTest server output = 'Begin Test br /' session = Session() test = Test(network+) session.add(test) output = output + str(session.new) + 'br /' session.commit() output = output + str(session.new) + 'br /' print session.dirty return self.header() + output + self.footer() def main(): cherrypy.quickstart(WebTest()) return 0 if __name__ == '__main__': main() END CODE --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more