Related
I'm using sqlalchemy to insert data into a table, but the error 'method' object is not iterable' is displayed in the console. How can I fix this? Here is my code:
with engine.connect() as conn:
conn.execute(
insert(text('users')).values(
{"fio": name, "datar": '11.08.1984', "id_role": randint(1, 2)}
))
conn.commit()
Here are the errors that are thrown:
File "C:\Users\gibso\PycharmProjects\test_task\main.py", line 41, in registration
conn.execute(
File "C:\Users\gibso\PycharmProjects\test_task\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1011, in execute
return meth(self, multiparams, params)
File "C:\Users\gibso\PycharmProjects\test_task\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 298, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\gibso\PycharmProjects\test_task\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1115, in _execute_clauseelement
compiled_sql = elem.compile(
File "<string>", line 1, in <lambda>
File "C:\Users\gibso\PycharmProjects\test_task\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 481, in compile
return self._compiler(dialect, bind=bind, **kw)
File "C:\Users\gibso\PycharmProjects\test_task\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 487, in _compiler
return dialect.statement_compiler(dialect, self, **kw)
File "C:\Users\gibso\PycharmProjects\test_task\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 592, in __init__
Compiled.__init__(self, dialect, statement, **kwargs)
File "C:\Users\gibso\PycharmProjects\test_task\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 322, in __init__
self.string = self.process(self.statement, **compile_kwargs)
File "C:\Users\gibso\PycharmProjects\test_task\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 352, in process
return obj._compiler_dispatch(self, **kwargs)
File "C:\Users\gibso\PycharmProjects\test_task\venv\lib\site-packages\sqlalchemy\sql\visitors.py", line 96, in _compiler_dispatch
return meth(self, **kw)
File "C:\Users\gibso\PycharmProjects\test_task\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 2467, in visit_insert
crud_params = crud._setup_crud_params(
File "C:\Users\gibso\PycharmProjects\test_task\venv\lib\site-packages\sqlalchemy\sql\crud.py", line 64, in _setup_crud_params
return _get_crud_params(compiler, stmt, **kw)
File "C:\Users\gibso\PycharmProjects\test_task\venv\lib\site-packages\sqlalchemy\sql\crud.py", line 158, in _get_crud_params
_scan_cols(
File "C:\Users\gibso\PycharmProjects\test_task\venv\lib\site-packages\sqlalchemy\sql\crud.py", line 316, in _scan_cols
for c in cols:
TypeError: 'method' object is not iterable
Since updating my Flask app's dependencies, my Sentry is logging errors I don't understand. Changes to Flask-Login or Flask-Security-Too have thrown me off.
My user model has always used an Integer as its primary_key
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
I'm getting the following error:
(psycopg2.errors.InvalidTextRepresentation) invalid input syntax for integer: "5818f9aad20447b1b6a37c51f2c2xxxx"
LINE 3: WHERE "user".id = '5818f9aad20447b1b6a37c51f2c2xxxx'
Which I assume stems from Flask-Security-Too's datastore user lookup:
def find_user(self, **kwargs):
return self.user_model.query.filter_by(**kwargs).first()
But maybe the problem goes bath further to Flask-Login as it's getting this random ID from the session:
# Load user from Flask Session
user_id = session.get("_user_id")
if user_id is not None and self._user_callback is not None:
user = self._user_callback(user_id)
Is Flask-Security-Too's fs_uniquifier in my session? Is that what's stepping into the place of my integer ID? I'm hoping I can avoid refactoring my entire code and database and retain my user.id as an int. Any thoughts are appreciated.
Here is the full trace that has occurred 15 times since upgrading, all without halting users from accessing or submitting data:
InvalidTextRepresentation: invalid input syntax for integer: "63b93cd01b8f4e65a7daa83becc3xxxx"
LINE 3: WHERE "user".id = '63b93cd01b8f4e65a7daa83becc3xxxx'
^
File "sqlalchemy/engine/base.py", line 1819, in _execute_context
self.dialect.do_execute(
File "sqlalchemy/engine/default.py", line 732, in do_execute
cursor.execute(statement, parameters)
DataError: (psycopg2.errors.InvalidTextRepresentation) invalid input syntax for integer: "63b93cd01b8f4e65a7daa83becc3xxxx"
LINE 3: WHERE "user".id = '63b93cd01b8f4e65a7daa83becc3xxxx'
^
[SQL: SELECT "user".id AS user_id, "user".school_id AS user_school_id, "user".location_id AS user_location_id, "user".first_name AS user_first_name, "user".last_name AS user_last_name, "user".display_name AS user_display_name, "user".image AS user_image, "user".email AS user_email, "user".password AS use...
File "flask/app.py", line 2463, in wsgi_app
response = self.full_dispatch_request()
File "flask/app.py", line 1760, in full_dispatch_request
rv = self.handle_user_exception(e)
File "__init__.py", line 271, in error_router
return original_handler(e)
File "flask/app.py", line 1756, in full_dispatch_request
rv = self.preprocess_request()
File "flask/app.py", line 2247, in preprocess_request
rv = self.ensure_sync(before_func)()
File "flask_principal.py", line 477, in _on_before_request
identity = loader()
File "flask_security/core.py", line 245, in _identity_loader
if not isinstance(current_user._get_current_object(), AnonymousUserMixin):
File "werkzeug/local.py", line 516, in _get_current_object
return get_name(local()) # type: ignore
File "flask_login/utils.py", line 25, in <lambda>
current_user = LocalProxy(lambda: _get_user())
File "flask_login/utils.py", line 372, in _get_user
current_app.login_manager._load_user()
File "flask_login/login_manager.py", line 364, in _load_user
user = self._user_callback(user_id)
File "flask_security/core.py", line 221, in _user_loader
return _security.datastore.find_user(id=user_id)
File "flask_security/datastore.py", line 254, in find_user
return self.user_model.query.filter_by(**kwargs).first()
File "sqlalchemy/orm/query.py", line 2819, in first
return self.limit(1)._iter().first()
File "sqlalchemy/orm/query.py", line 2903, in _iter
result = self.session.execute(
File "sqlalchemy/orm/session.py", line 1712, in execute
result = conn._execute_20(statement, params or {}, execution_options)
File "sqlalchemy/engine/base.py", line 1631, in _execute_20
return meth(self, args_10style, kwargs_10style, execution_options)
File "sqlalchemy/sql/elements.py", line 332, in _execute_on_connection
return connection._execute_clauseelement(
File "sqlalchemy/engine/base.py", line 1498, in _execute_clauseelement
ret = self._execute_context(
File "sqlalchemy/engine/base.py", line 1862, in _execute_context
self._handle_dbapi_exception(
File "sqlalchemy/engine/base.py", line 2043, in _handle_dbapi_exception
util.raise_(
File "sqlalchemy/util/compat.py", line 208, in raise_
raise exception
File "sqlalchemy/engine/base.py", line 1819, in _execute_context
self.dialect.do_execute(
File "sqlalchemy/engine/default.py", line 732, in do_execute
cursor.execute(statement, parameters)
InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block
File "sqlalchemy/engine/base.py", line 1819, in _execute_context
self.dialect.do_execute(
File "sqlalchemy/engine/default.py", line 732, in do_execute
cursor.execute(statement, parameters)
InternalError: (psycopg2.errors.InFailedSqlTransaction) current transaction is aborted, commands ignored until end of transaction block
[SQL: SELECT "user".id AS user_id, "user".school_id AS user_school_id, "user".location_id AS user_location_id, "user".first_name AS user_first_name, "user".last_name AS user_last_name, "user".display_name AS user_display_name, "user".image AS user_image, "user".email AS user_email, "user".password AS user_password, "user".fs_uniquifier AS user_fs_uniquifier, "user".phone AS user_phone...
File "flask/app.py", line 2486, in __call__
return self.wsgi_app(environ, start_response)
File "flask/app.py", line 2466, in wsgi_app
response = self.handle_exception(e)
File "__init__.py", line 271, in error_router
return original_handler(e)
File "flask/app.py", line 1662, in handle_exception
server_error = self.ensure_sync(handler)(server_error)
File "__init__.py", line 21, in crash_page
return render_template('error_pages/500.html'), 500
File "flask/templating.py", line 147, in render_template
return _render(app, template, context)
File "flask/templating.py", line 128, in _render
app.update_template_context(context)
File "flask/app.py", line 932, in update_template_context
context.update(func())
File "flask_login/utils.py", line 407, in _user_context_processor
return dict(current_user=_get_user())
File "flask_login/utils.py", line 372, in _get_user
current_app.login_manager._load_user()
File "flask_login/login_manager.py", line 364, in _load_user
user = self._user_callback(user_id)
File "flask_security/core.py", line 221, in _user_loader
return _security.datastore.find_user(id=user_id)
File "flask_security/datastore.py", line 254, in find_user
return self.user_model.query.filter_by(**kwargs).first()
File "sqlalchemy/orm/query.py", line 2819, in first
return self.limit(1)._iter().first()
File "sqlalchemy/orm/query.py", line 2903, in _iter
result = self.session.execute(
File "sqlalchemy/orm/session.py", line 1712, in execute
result = conn._execute_20(statement, params or {}, execution_options)
File "sqlalchemy/engine/base.py", line 1631, in _execute_20
return meth(self, args_10style, kwargs_10style, execution_options)
File "sqlalchemy/sql/elements.py", line 332, in _execute_on_connection
return connection._execute_clauseelement(
File "sqlalchemy/engine/base.py", line 1498, in _execute_clauseelement
ret = self._execute_context(
File "sqlalchemy/engine/base.py", line 1862, in _execute_context
self._handle_dbapi_exception(
File "sqlalchemy/engine/base.py", line 2043, in _handle_dbapi_exception
util.raise_(
File "sqlalchemy/util/compat.py", line 208, in raise_
raise exception
File "sqlalchemy/engine/base.py", line 1819, in _execute_context
self.dialect.do_execute(
File "sqlalchemy/engine/default.py", line 732, in do_execute
cursor.execute(statement, parameters)
Some additional info - as of FS2 3.4.0 it no longer references user.id at all - so that can be whatever you want. FS2 overrides flask_login's UserMixin::get_id - and returns the value of fs_uniquifier. And yes - that is what is stored in the session cookie - and no - nothing in the session cookie is encrypted (it is signed).
So: 1) are you overriding get_id()?
2) any chance that since it seems to be 'working' that you have some post-processing - maybe in a signal or something and it is calling the find_user or some other DB query?
Facing the following error in a Pyramid Application while using SQLAlchemy and Zope Transaction Manager.
This is how I am creating a scoped session:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.schema import MetaData
from sqlalchemy.orm import (
scoped_session,
sessionmaker,
)
from zope.sqlalchemy import ZopeTransactionExtension
metadata = MetaData(naming_convention=NAMING_CONVENTION)
Base = declarative_base(metadata=metadata)
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension(keep_session=True)))
I am syncing my session creation and removal with my requests in the following way:
#view_config(route_name='social_get_individual_assets', renderer='json', effective_principals=2, permission='edit')
def social_get_individual_assets(requestJson):
session = DBSession()
try:
body = requestJson.request.json_body
search_term = body['text']
horizontal = body['horizontal']
vertical = body['vertical']
log.info("social get individual assets request: %s", str(search_term).encode(encoding='utf_8'))
json_data = get_individual_assets(session, search_term, horizontal, vertical)
log.info("social get assets response: %s", str(search_term).encode(encoding='utf_8'))
transaction.commit()
DBSession.remove()
return json_data
except Exception as e:
session.rollback()
log.exception(e)
raise e
For some reason, I keep running into this error all the time:
2017-12-06 13:32:07,965 ERROR [invideoapp.views.default:465] An operation previously failed, with traceback:
File "/usr/lib64/python3.5/threading.py", line 882, in _bootstrap
self._bootstrap_inner()
File "/usr/lib64/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib64/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.5/site-packages/waitress/task.py", line 78, in handler_thread
task.service()
File "/usr/local/lib/python3.5/site-packages/waitress/channel.py", line 338, in service
task.service()
File "/usr/local/lib/python3.5/site-packages/waitress/task.py", line 169, in service
self.execute()
File "/usr/local/lib/python3.5/site-packages/waitress/task.py", line 399, in execute
app_iter = self.channel.server.application(env, start_response)
File "/usr/local/lib/python3.5/site-packages/pyramid/router.py", line 270, in __call__
response = self.execution_policy(environ, self)
File "/usr/local/lib/python3.5/site-packages/pyramid_retry/__init__.py", line 114, in retry_policy
response = router.invoke_request(request)
File "/usr/local/lib/python3.5/site-packages/pyramid/router.py", line 249, in invoke_request
response = handle_request(request)
File "/usr/local/lib/python3.5/site-packages/pyramid_tm/__init__.py", line 136, in tm_tween
response = handler(request)
File "/usr/local/lib/python3.5/site-packages/pyramid/tweens.py", line 39, in excview_tween
response = handler(request)
File "/usr/local/lib/python3.5/site-packages/pyramid/router.py", line 156, in handle_request
view_name
File "/usr/local/lib/python3.5/site-packages/pyramid/view.py", line 642, in _call_view
response = view_callable(context, request)
File "/usr/local/lib/python3.5/site-packages/pyramid/viewderivers.py", line 439, in rendered_view
result = view(context, request)
File "/usr/local/lib/python3.5/site-packages/pyramid/viewderivers.py", line 148, in _requestonly_view
response = view(request)
File "/home/ttv/invideoapp/invideoapp/views/default.py", line 148, in upload_image
transaction.commit()
File "/usr/local/lib/python3.5/site-packages/transaction/_manager.py", line 131, in commit
return self.get().commit()
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 308, in commit
t, v, tb = self._saveAndGetCommitishError()
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 301, in commit
self._commitResources()
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 446, in _commitResources
reraise(t, v, tb)
File "/usr/local/lib/python3.5/site-packages/transaction/_compat.py", line 54, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 423, in _commitResources
rm.tpc_vote(self)
File "/usr/local/lib/python3.5/site-packages/zope/sqlalchemy/datamanager.py", line 109, in tpc_vote
self.tx.commit()
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 459, in commit
self._assert_active(prepared_ok=True)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 285, in _assert_active
raise sa_exc.ResourceClosedError(closed_msg)
sqlalchemy.exc.ResourceClosedError: This transaction is closed
Traceback (most recent call last):
File "/home/ttv/invideoapp/invideoapp/views/default.py", line 451, in update_master_json
user = getUser(session,user_id)
File "/home/ttv/invideoapp/invideoapp/invideomodules/auth_processing.py", line 12, in getUser
raise e
File "/home/ttv/invideoapp/invideoapp/invideomodules/auth_processing.py", line 8, in getUser
query = session.query(User).filter(User.user_id == userid).first()
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/query.py", line 2755, in first
ret = list(self[0:1])
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/query.py", line 2547, in __getitem__
return list(res)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/query.py", line 2855, in __iter__
return self._execute_and_instances(context)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/query.py", line 2876, in _execute_and_instances
close_with_result=True)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/query.py", line 2885, in _get_bind_args
**kw
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/query.py", line 2867, in _connection_from_session
conn = self.session.connection(**kw)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 966, in connection
execution_options=execution_options)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 971, in _connection_for_bind
engine, execution_options)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 417, in _connection_for_bind
self.session.dispatch.after_begin(self.session, self, conn)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/event/attr.py", line 256, in __call__
fn(*args, **kw)
File "/usr/local/lib/python3.5/site-packages/zope/sqlalchemy/datamanager.py", line 237, in after_begin
join_transaction(session, self.initial_state, self.transaction_manager, self.keep_session)
File "/usr/local/lib/python3.5/site-packages/zope/sqlalchemy/datamanager.py", line 211, in join_transaction
DataManager(session, initial_state, transaction_manager, keep_session=keep_session)
File "/usr/local/lib/python3.5/site-packages/zope/sqlalchemy/datamanager.py", line 73, in __init__
transaction_manager.get().join(self)
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 179, in join
self._prior_operation_failed() # doesn't return
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 173, in _prior_operation_failed
self._failure_traceback.getvalue())
transaction.interfaces.TransactionFailedError: An operation previously failed, with traceback:
File "/usr/lib64/python3.5/threading.py", line 882, in _bootstrap
self._bootstrap_inner()
File "/usr/lib64/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib64/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.5/site-packages/waitress/task.py", line 78, in handler_thread
task.service()
File "/usr/local/lib/python3.5/site-packages/waitress/channel.py", line 338, in service
task.service()
File "/usr/local/lib/python3.5/site-packages/waitress/task.py", line 169, in service
self.execute()
File "/usr/local/lib/python3.5/site-packages/waitress/task.py", line 399, in execute
app_iter = self.channel.server.application(env, start_response)
File "/usr/local/lib/python3.5/site-packages/pyramid/router.py", line 270, in __call__
response = self.execution_policy(environ, self)
File "/usr/local/lib/python3.5/site-packages/pyramid_retry/__init__.py", line 114, in retry_policy
response = router.invoke_request(request)
File "/usr/local/lib/python3.5/site-packages/pyramid/router.py", line 249, in invoke_request
response = handle_request(request)
File "/usr/local/lib/python3.5/site-packages/pyramid_tm/__init__.py", line 136, in tm_tween
response = handler(request)
File "/usr/local/lib/python3.5/site-packages/pyramid/tweens.py", line 39, in excview_tween
response = handler(request)
File "/usr/local/lib/python3.5/site-packages/pyramid/router.py", line 156, in handle_request
view_name
File "/usr/local/lib/python3.5/site-packages/pyramid/view.py", line 642, in _call_view
response = view_callable(context, request)
File "/usr/local/lib/python3.5/site-packages/pyramid/viewderivers.py", line 439, in rendered_view
result = view(context, request)
File "/usr/local/lib/python3.5/site-packages/pyramid/viewderivers.py", line 148, in _requestonly_view
response = view(request)
File "/home/ttv/invideoapp/invideoapp/views/default.py", line 148, in upload_image
transaction.commit()
File "/usr/local/lib/python3.5/site-packages/transaction/_manager.py", line 131, in commit
return self.get().commit()
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 308, in commit
t, v, tb = self._saveAndGetCommitishError()
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 301, in commit
self._commitResources()
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 446, in _commitResources
reraise(t, v, tb)
File "/usr/local/lib/python3.5/site-packages/transaction/_compat.py", line 54, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/transaction/_transaction.py", line 423, in _commitResources
rm.tpc_vote(self)
File "/usr/local/lib/python3.5/site-packages/zope/sqlalchemy/datamanager.py", line 109, in tpc_vote
self.tx.commit()
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 459, in commit
self._assert_active(prepared_ok=True)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/orm/session.py", line 285, in _assert_active
raise sa_exc.ResourceClosedError(closed_msg)
sqlalchemy.exc.ResourceClosedError: This transaction is closed
Once I get this error, none of my Database connection stuff works. Only way out is to restart the entire application to make things work. Any idea why this is happening?
I had a similar problem, and I found using DBSession.begin_nested() solved the issue. So something like:
#view_config(route_name='social_get_individual_assets', renderer='json', effective_principals=2, permission='edit')
def social_get_individual_assets(requestJson):
try:
DBSession.begin_nested()
body = requestJson.request.json_body
search_term = body['text']
horizontal = body['horizontal']
vertical = body['vertical']
log.info("social get individual assets request: %s", str(search_term).encode(encoding='utf_8'))
json_data = get_individual_assets(session, search_term, horizontal, vertical)
log.info("social get assets response: %s", str(search_term).encode(encoding='utf_8'))
return json_data
except Exception as e:
DBSession.rollback()
log.exception(e)
raise e
I am trying to make a script which is importing data to mssql from Excel.
I read data from Excel and assigned to variables. It works, I'm checking by printing it. Connecting to database and inserting string works correctly, too. But it doesn't insert variables. Please help me to find a solution.
python script:
for rownum in range(1,sheet.nrows):
if sheet.row_values(rownum)[0]=='':
pass
else:
vals=[sheet.row_values(rownum)]
val = sheet.cell_value(rownum, 1)
val2=sheet.cell_value(rownum, 2)
val3=sheet.cell_value(rownum, 3)
val1=sheet.cell_value(rownum, 0)
val4=sheet.cell_value(rownum, 4)
val5=sheet.cell_value(rownum, 5)
val1= val1.encode("utf-8")
val4 = val4.encode("utf-8")
val5 = val5.encode("utf-8")
dat=int(val)
dt=date.fromordinal(date(1990,1,1).toordinal()+dat-2)
x = int(val2 * 24 * 3600)
x3 = int(val3 * 24 * 3600)# convert to number of seconds
m = time(x//3600, (x%3600)//60, x%60)
m3 = time(x3//3600, (x3%3600)//60, x3%60)
cursor.executemany ("INSERT INTO [sample.dbo.tv_prot_virt_im](chan_name,pr_date,start_time,end_time,prog_name,pr_genre) VALUES(?,?,?,?,?,?)", [val1,dt,m,m3,val4,val5])
connection.commit()
Traceback (most recent call last):
File "C:/Users/akylyshbekova/Desktop/PyQT/main_test.py", line 26, in <module>
tab = Table('dbo.tv_prot_virt_im',metadata,autoload=True)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\sql\schema.py", line 439, in __new__
metadata._remove_table(name, schema)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\sql\schema.py", line 434, in __new__
table._init(name, metadata, *args, **kw)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\sql\schema.py", line 514, in _init
include_columns, _extend_on=_extend_on)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\sql\schema.py", line 540, in _autoload
_extend_on=_extend_on
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 2044, in run_callable
with self.contextual_connect() as conn:
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 2112, in contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 2151, in _wrap_pool_connect
e, dialect, self)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 1465, in _handle_dbapi_exception_noconnection
exc_info
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\util\compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\base.py", line 2147, in _wrap_pool_connect
return fn()
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 387, in connect
return _ConnectionFairy._checkout(self)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 516, in checkout
rec = pool._do_get()
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 1138, in _do_get
self._dec_overflow()
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 1135, in _do_get
return self._create_connection()
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "C:\ProgramData\Anaconda2\lib\site-packages\sqlalchemy\engine\default.py", line 393, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('IM002', '[IM002] [Microsoft][\xc4\xe8\xf1\xef\xe5\xf2\xf7\xe5\xf0 \xe4\xf0\xe0\xe9\xe2\xe5\xf0\xee\xe2 ODBC] \xc8\xf1\xf2\xee\xf7\xed\xe8\xea \xe4\xe0\xed\xed\xfb\xf5 \xed\xe5 \xed\xe0\xe9\xe4\xe5\xed \xe8 \xed\xe5 \xf3\xea\xe0\xe7\xe0\xed \xe4\xf0\xe0\xe9\xe2\xe5\xf0, \xe8\xf1\xef\xee\xeb\xfc\xe7\xf3\xe5\xec\xfb\xe9 \xef\xee \xf3\xec\xee\xeb\xf7\xe0\xed\xe8\xfe (0) (SQLDriverConnect)')
Process finished with exit code 1
I have an enum... defined very similarly to here.
When I try to create a table using the enum, as in the example, I get an error during table creation. It is a very vague error. Here is code that replicates the problem:
from sqlalchemy import Table, MetaData, Column, Enum, create_engine
import enum
class myEnum(enum.Enum):
one = 'one'
two = 'two'
three = 'three'
def main():
e = create_engine('sqlite:///:memory:')
e.echo = True
m = MetaData(bind = e)
t = Table('table', m, Column('my_enum', Enum(myEnum)))
t.create()
if __name__ == '__main__': main()
I get an AttributeError: replace when I run this code, with a seemingly useless stacktrace. I honestly don't even know where to begin debugging this and while I can conceive of a couple of possible workarounds, I am relatively new to SQLAlchemy so I'm not sure what the cleanest solution is. Surely basic enum support is a relatively simple expectation of a ORM framework?
Here is the full stack trace:
Traceback (most recent call last):
File "table_test.py", line 17, in <module>
if __name__ == '__main__': main()
File "table_test.py", line 16, in main
t.create()
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\schema.py", line 725, in create
checkfirst=checkfirst)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1856, in _run_visitor
conn._run_visitor(visitorcallable, element, **kwargs)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1481, in _run_visitor
**kwargs).traverse_single(element)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 121, in traverse_single
return meth(obj, **kw)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\ddl.py", line 764, in visit_table
include_foreign_key_constraints=include_foreign_key_constraints
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 914, in execute
return meth(self, multiparams, params)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\ddl.py", line 68, in _execute_on_connection
return connection._execute_ddl(self, multiparams, params)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 962, in _execute_ddl
compiled = ddl.compile(dialect=dialect)
File "<string>", line 1, in <lambda>
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\elements.py", line 494, in compile
return self._compiler(dialect, bind=bind, **kw)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\ddl.py", line 26, in _compiler
return dialect.ddl_compiler(dialect, self, **kw)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 190, in __init__
self.string = self.process(self.statement, **compile_kwargs)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 213, in process
return obj._compiler_dispatch(self, **kwargs)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 2173, in visit_create_table
create.include_foreign_key_constraints)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 2220, in create_table_constraints
for constraint in constraints
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 2218, in <genexpr>
p for p in
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 2226, in <genexpr>
not getattr(constraint, 'use_alter', False)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 213, in process
return obj._compiler_dispatch(self, **kwargs)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 93, in _compiler_dispatch
return meth(self, **kw)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 2369, in visit_check_constraint
literal_binds=True)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 213, in process
return obj._compiler_dispatch(self, **kwargs)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 927, in visit_binary
return self._generate_generic_binary(binary, opstring, **kw)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 944, in _generate_generic_binary
binary.right._compiler_dispatch(self, **kw)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 527, in visit_grouping
return "(" + grouping.element._compiler_dispatch(self, **kwargs) + ")"
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 751, in visit_clauselist
for c in clauselist.clauses)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 748, in <genexpr>
s for s in
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 751, in <genexpr>
for c in clauselist.clauses)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 1071, in visit_bindparam
bindparam, within_columns_clause=True, **kwargs)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 1103, in render_literal_bindparam
return self.render_literal_value(value, bindparam.type)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 1118, in render_literal_value
return processor(value)
File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\sqltypes.py", line 171, in process
value = value.replace("'", "''")
File "C:\Anaconda3\lib\enum.py", line 268, in __getattr__
raise AttributeError(name) from None
AttributeError: replace
PEP-435 enum support is being added in 1.1.
1.1.0b1 was recently released. You can upgrade to 1.1.0b1 but be wary of bugs. Based on release history I would say the stable version should be released in a month or two.
how to upgrade:
pip install 'sqlalchemy==1.1.0b3'