Skip to content

Instantly share code, notes, and snippets.

@robbles
Created March 5, 2012 19:45
Show Gist options
  • Save robbles/1980584 to your computer and use it in GitHub Desktop.
Save robbles/1980584 to your computer and use it in GitHub Desktop.
SQLAlchemy raises None, causes TypeError - Test
Traceback (most recent call last):
File "test_case_9525220.py", line 41, in <module>
session.commit()
File "/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 645, in commit
self.transaction.commit()
File "/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 313, in commit
self._prepare_impl()
File "/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 297, in _prepare_impl
self.session.flush()
File "/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1547, in flush
self._flush(objects)
File "/usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1635, in _flush
raise
TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType
# For http://stackoverflow.com/questions/9525220/sqlalchemy-raises-none-causes-typeerror
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import sessionmaker
import MySQLdb
Base = declarative_base()
CONNECTION_TYPE = 'mysql_db_pool'
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True, autoincrement=True)
userid = Column(String(50), nullable=False)
if __name__ == '__main__':
url = 'mysql://root:[email protected]:3306/testdb'
if CONNECTION_TYPE == 'sqlite':
engine = create_engine('sqlite:///:memory:', echo=True)
elif CONNECTION_TYPE == 'mysql':
engine = create_engine(url)
elif CONNECTION_TYPE == 'mysql_db_pool':
from eventlet.db_pool import ConnectionPool
pool = ConnectionPool(MySQLdb, host='127.0.0.1', user='root', passwd='root', db='testdb')
engine = create_engine(url,
creator=pool.create,
pool_size=pool.max_size,
)
session = sessionmaker(bind=engine)()
User.metadata.drop_all(bind=engine)
User.metadata.create_all(bind=engine)
u = User()
session.add(u)
session.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment