sqlalchemy.exc.NoSuchColumnError:"找不到列中的列' 0'"

时间:2017-03-02 20:54:54

标签: python sql sqlalchemy psycopg2

所以我已经使用来自linux服务器的python连接到redshift。 它抛出了sqlalchemy.exc.NoSuchColumnError:"找不到列中的列' 0'"我尝试从redshift服务器获取表时出错。

其他经历过类似错误的错误通过更新python包解决了这个问题,我今天早些时候做了很多,但我仍然遇到错误。

版本:

Python 2.7.12

SQLAlchemy 1.1.6

sqlalchemy-redshift 0.5.0

psycopg2 2.7

这是代码,我使用的是console_login:

from sqlalchemy import create_engine
import getpass
import json
def getredshiftconnect(console_login=True, login_credentials_file=None):
try:
    if console_login:
        login = getpass.getpass("Username: ")
        password = getpass.getpass("Password: ")
        server = input("Server: ")
        database = input("Database connecting to: ")
        schema = input("Schema connecting to: ")
        engine = create_engine("redshift+psycopg2://{0}:{1}@{2}:<PortNumber>/{3}"\
                                   .format(login, password, server, database))
    else:
        # Read JSON file with login credentials
        # Make sure that fields are referenced correctly, i.e.:
        # login, password, server, database
        login_credentials = json.load(open(login_credentials_file,"r"))
        engine = create_engine("redshift+psycopg2://{0}:{1}@{2}:<PortNumber>/{3}"\
                                   .format(login_credentials["login"],
                                           login_credentials["password"],
                                           login_credentials["server"],
                                           login_credentials["database"]))
    # To test connection, if able to list tables then login was succesful
    # Pick a schema that actually has tables
    if engine.table_names(schema): # Explodes at this call
        print ("Successfully connected")
        return engine
    else:
        print ("Not properly connected")
except Exception as e:
    print ("Error Connecting: ", e)

1 个答案:

答案 0 :(得分:3)

我确实发现SQLAlchemy-1.1.6是问题的原因。 在降级到1.1.5之后,我的情况再次正常工作。

pip install SQLAlchemy==1.1.5

注意:我发现SQLAlchemy的最新版本有更多错误。 此外,我不得不在某些服务器上删除并重新安装SQLAlchemy来解决问题(例如pip uninstall SQLAlchemy)。