VARCHAR column is not compatible with autoincrement=True - python

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Sequence, Integer, Float, String, Boolean, ForeignKey, TIMESTAMP, Enum
import datetime
from os import urandom
from binascii import hexlify
from hashlib import sha1
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:neljaD44#localhost:5432/postgress";
db = SQLAlchemy(app)
After running this file I get following error.
python2.7 create_postgresql_database.py
/usr/local/lib/python2.7/dist-packages/flask_sqlalchemy/__init__.py:873: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Traceback (most recent call last):
File "create_postgresql_database.py", line 903, in <module>
db.create_all()
File "/usr/local/lib/python2.7/dist-packages/flask_sqlalchemy/__init__.py", line 1094, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/usr/local/lib/python2.7/dist-packages/flask_sqlalchemy/__init__.py", line 1086, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", line 4745, in create_all
ddl.SchemaGenerator, self, checkfirst=checkfirst, tables=tables
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 3079, in _run_ddl_visitor
conn._run_ddl_visitor(visitorcallable, element, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2081, in _run_ddl_visitor
visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 485, in traverse_single
return meth(obj, **kw)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/ddl.py", line 850, in visit_metadata
_is_metadata_operation=True,
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 485, in traverse_single
return meth(obj, **kw)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/ddl.py", line 895, in visit_table
include_foreign_key_constraints, # noqa
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1262, in execute
return meth(self, multiparams, params, _EMPTY_EXECUTION_OPTS)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/ddl.py", line 78, in _execute_on_connection
self, multiparams, params, execution_options
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1350, in _execute_ddl
dialect=dialect, schema_translate_map=schema_translate_map
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/elements.py", line 522, in compile
return self._compiler(dialect, **kw)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/ddl.py", line 29, in _compiler
return dialect.ddl_compiler(dialect, self, **kw)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 455, in __init__
self.string = self.process(self.statement, **compile_kwargs)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 490, in process
return obj._compiler_dispatch(self, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 4025, in visit_create_table
create_column, first_pk=column.primary_key and not first_pk
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 490, in process
return obj._compiler_dispatch(self, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 4058, in visit_create_column
text = self.get_column_specification(column, first_pk=first_pk)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/dialects/postgresql/base.py", line 2474, in get_column_specification
and column is column.table._autoincrement_column
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", line 810, in _autoincrement_column
return self.primary_key._autoincrement_column
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 1097, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", line 3954, in _autoincrement_column
_validate_autoinc(col, True)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", line 3928, in _validate_autoinc
"compatible with autoincrement=True" % (col.type, col)
sqlalchemy.exc.ArgumentError: Column type VARCHAR(64) on column 'sessions.session_id' is not compatible with autoincrement=True
root#bdnewsbdnews:/home/khondakar/Desktop#

You cannot have a VARCHAR column set to autoincrement = True. You should use an INTEGER column (for instance, most common) instead.

Related

'method' object is not iterable when trying to add data to database

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

sqlalchemy.exc.ResourceClosedError: This transaction is closed

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

Unknown PG numeric type 25

I am working on a project with Python-Flask, SQL Alchemy, & Postgres qith psycopg2 package.
The project's save the data in database successfully, but throws error "Unknown PG numeric type 25" while refreshing object.
project.second_payment_percent = int(request.form.get("second_payment_percent", 0)) if request.form.get("second_payment_percent", 0) else 0
project.second_payment_before = int(request.form.get("second_payment_before", 120))
project.currency_id = int(request.form.get("currency_code"))
project.security_deposit = int(request.form.get("security_deposit", 0))
project.apa = int(request.form.get("apa", 0))
project.vat_schema_id = request.form.get("vat_schema") if request.form.get("vat_schema", 0) else None
if project.apa > 0:
project.security_deposit = int(project.project_price) * int(project.apa) / 100
db.session.add(project)
db.session.commit()
db.session.refresh(project)
db.session.flush()
The error I get is
File "<project directory>utils/app_views.py", line 56, in decorated
return f(*args, **kwargs)
File "<project directory>views/admin/project.py", line 347, in admin_products_save_accommodation
db.session.refresh(project)
File "<project directory>models/project.py", line 297, in serialize
reviews, avg_rating = self.reviews
File "<project directory>models/project.py", line 269, in reviews
robj = db.session.query(ProjectReviews).filter_by(product_id=self.id).order_by('time_created').all()
File "<venv path>lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 237, in __get__
return self.impl.get(instance_state(instance), dict_)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 578, in get
value = state._load_expired(state, passive)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/orm/state.py", line 474, in _load_expired
self.manager.deferred_scalar_loader(self, toload)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 664, in load_scalar_attributes
only_load_props=attribute_names)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 219, in load_on_ident
return q.one()
File "<venv path>lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2693, in one
ret = list(self)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2736, in __iter__
return self._execute_and_instances(context)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2751, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 914, in execute
return meth(self, multiparams, params)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 323, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1010, in _execute_clauseelement
compiled_sql, distilled_params
File "<venv path>lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1161, in _execute_context
result = context.get_result_proxy()
File "<venv path>lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 474, in get_result_proxy
return _result.ResultProxy(self)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/engine/result.py", line 495, in __init__
self._init_metadata()
File "<venv path>lib/python2.7/site-packages/sqlalchemy/engine/result.py", line 524, in _init_metadata
self._metadata = ResultMetaData(self, metadata)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/engine/result.py", line 221, in __init__
in enumerate(result_columns)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 764, in get_result_processor
return type_._cached_result_processor(self.dialect, coltype)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/sql/type_api.py", line 392, in _cached_result_processor
d[coltype] = rp = d['impl'].result_processor(dialect, coltype)
File "<venv path>lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 361, in result_processor
"Unknown PG numeric type: %d" % coltype)
InvalidRequestError: Unknown PG numeric type: 25
I have tried many things, but failed.
I guess one of the fields in your table is a text and you are trying to retrieve it as a number.
postgres=# select * from pg_type where oid = 25;
-[ RECORD 1 ]--+---------
typname | text

blaze-odo error when trying to export an odo result to csv with MSSQL backend

I'm having an issue exporting some data from a MSSQL back-end to csv using blaze and odo.
The first thing that I do is create a blaze.data object that connects to the MSSQL db using a dns connection and pyodbc as the driver.
adjustment_statuses is a table in the database that the blaze.data object is connected to.
Then I use odo to take that result set and output it to a csv. When I output to a json format it works as expected. My hunch is that there is some issue with mssql support as an odo backend. Any odo devs have any insight here?
ez = bz.data(EZ_DB)
result = ez.adjustment_statuses
odo(result, os.path.join(ETL_STAGING_LOC, 'ez_adjustment.csv'))
Traceback (most recent call last):
File "<input>", line 25, in <module>
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\odo\odo.py", line 91, in odo
return into(target, source, **kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\multipledispatch\dispatcher.py", line 164, in __call__
return func(*args, **kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\blaze\interactive.py", line 406, in into
return into(a, result, **kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\multipledispatch\dispatcher.py", line 164, in __call__
return func(*args, **kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\odo\into.py", line 43, in wrapped
return f(*args, **kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\odo\into.py", line 143, in into_string
return into(a, b, dshape=dshape, **kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\multipledispatch\dispatcher.py", line 164, in __call__
return func(*args, **kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\odo\into.py", line 43, in wrapped
return f(*args, **kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\odo\into.py", line 131, in into_object
return append(target, source, dshape=dshape, **kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\multipledispatch\dispatcher.py", line 164, in __call__
return func(*args, **kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\odo\backends\sql.py", line 808, in append_table_to_csv
conn.execute(stmt)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\sqlalchemy\engine\base.py", line 945, in execute
return meth(self, multiparams, params)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\sqlalchemy\sql\elements.py", line 263, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\sqlalchemy\engine\base.py", line 1046, in _execute_clauseelement
if not self.schema_for_object.is_default else None)
File "<string>", line 1, in <lambda>
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\sqlalchemy\sql\elements.py", line 436, in compile
return self._compiler(dialect, bind=bind, **kw)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\sqlalchemy\sql\elements.py", line 442, in _compiler
return dialect.statement_compiler(dialect, self, **kw)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\sqlalchemy\dialects\mssql\base.py", line 1107, in __init__
super(MSSQLCompiler, self).__init__(*args, **kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\sqlalchemy\sql\compiler.py", line 435, in __init__
Compiled.__init__(self, dialect, statement, **kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\sqlalchemy\sql\compiler.py", line 216, in __init__
self.string = self.process(self.statement, **compile_kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\sqlalchemy\sql\compiler.py", line 242, in process
return obj._compiler_dispatch(self, **kwargs)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\sqlalchemy\ext\compiler.py", line 435, in <lambda>
lambda *arg, **kw: existing(*arg, **kw))
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\sqlalchemy\ext\compiler.py", line 474, in __call__
return fn(element, compiler, **kw)
File "C:\Users\jgenser\AppData\Local\Continuum\Anaconda2\envs\etl\lib\site-packages\sqlalchemy\ext\compiler.py", line 430, in _wrap_existing_dispatch
"compilation handler." % type(element))
CompileError: <class 'odo.backends.sql.CopyToCSV'> construct has no default compilation handler.

SQLAlchemy column with enum using PEP 435 compliant enum produces table creation error

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'

Categories

Resources