Column does not exist error on CreateTable

久未见 提交于 2021-02-11 15:08:39

问题


I'm trying to print out DDLs for all the tables in Redshift database. My code look something like this:

cnxn = engine.connect()
metadata = MetaData(bind=engine, schema=SCHEMA)
metadata.reflect(bind=engine)
for table in metadata.sorted_tables:
    try:
        res = CreateTable(table).compile(engine)
        print(res)
    except ProgrammingError as e: 
        print(f"Failed to compile DDL for {table}: {e}")
        continue

I've tested this code on other redshift databases and it seems to work fine. But in this database it fails and gives me this huge traceback:

Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1277, in _execute_context
    self.dialect.do_execute(
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\default.py", line 593, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.UndefinedColumn: column c.reldiststyle does not exist
LINE 8:           CASE c.reldiststyle
                       ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ".\extract_ddl.py", line 31, in <module>
    metadata.reflect(bind=engine)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\schema.py", line 4458, in reflect      
    bind.engine.table_names(schema, connection=conn)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 2320, in table_names 
    return self.dialect.get_table_names(conn, schema)
  File "<string>", line 2, in get_table_names
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\reflection.py", line 44, in cache   
    return fn(self, con, *args, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy_redshift\dialect.py", line 532, in get_table_names
    return self._get_table_or_view_names('r', connection, schema, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy_redshift\dialect.py", line 651, in _get_table_or_view_names
    all_relations = self._get_all_relation_info(connection,
  File "<string>", line 2, in _get_all_relation_info
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\reflection.py", line 44, in cache   
    return fn(self, con, *args, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy_redshift\dialect.py", line 716, in _get_all_relation_info
    result = connection.execute("""
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1006, in execute     
    return self._execute_text(object_, multiparams, params)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1175, in _execute_texPS D:\Work\Python> python .\extract_ddl.py
Connected to redshiftCreateEngine
Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1277, in _execute_context
    self.dialect.do_execute(
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\default.py", line 593, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.UndefinedColumn: column c.reldiststyle does not exist
LINE 8:           CASE c.reldiststyle
                       ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ".\extract_ddl.py", line 32, in <module>
    metadata.reflect(bind=engine)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\schema.py", line 4458, in reflect      
    bind.engine.table_names(schema, connection=conn)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 2320, in table_names 
    return self.dialect.get_table_names(conn, schema)
  File "<string>", line 2, in get_table_names
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\reflection.py", line 44, in cache   
    return fn(self, con, *args, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy_redshift\dialect.py", line 532, in get_table_names
    return self._get_table_or_view_names('r', connection, schema, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy_redshift\dialect.py", line 651, in _get_table_or_view_names
    all_relations = self._get_all_relation_info(connection,
  File "<string>", line 2, in _get_all_relation_info
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\reflection.py", line 44, in cache   
    return fn(self, con, *args, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy_redshift\dialect.py", line 716, in _get_all_relation_info
    result = connection.execute("""
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1006, in execute     
    return self._execute_text(object_, multiparams, params)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1175, in _execute_tex    ret = self._execute_context(
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1317, in _execute_context
    self._handle_dbapi_exception(
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1511, in _handle_dbapi_exception
    util.raise_(
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\util\compat.py", line 178, in raise_       
    raise exception
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1277, in _execute_context
    self.dialect.do_execute(
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\default.py", line 593, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedColumn) column c.reldiststyle does not exist
LINE 8:           CASE c.reldiststyle
                       ^

[SQL:
        SELECT
          c.relkind,
          n.oid as "schema_oid",
          n.nspname as "schema",
          c.oid as "rel_oid",
          c.relname,
          CASE c.reldiststyle
            WHEN 0 THEN 'EVEN' WHEN 1 THEN 'KEY' WHEN 8 THEN 'ALL' END
            AS "diststyle",
          c.relowner AS "owner_id",
          u.usename AS "owner_name",
          TRIM(TRAILING ';' FROM pg_catalog.pg_get_viewdef(c.oid, true))
            AS "view_definition",
          pg_catalog.array_to_string(c.relacl, '
') AS "privileges"
        FROM pg_catalog.pg_class c
             LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
             JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner
        WHERE c.relkind IN ('r', 'v', 'm', 'S', 'f')
          AND n.nspname !~ '^pg_'
        ORDER BY c.relkind, n.oid, n.nspname;
        ]
(Background on this error at: http://sqlalche.me/e/13/f405)

Now there are almost a hundred tables in database where this error occurs. I want to just skip the table where this exception occurs and continue on to the next table. What am I doing wrong here? Also, can I do something about this error?

Update: On further inspection it seems that the exception occurs when sqlalchemy is reflecting the table metadata.reflect(bind=engine), not in the for loop. So, How do I prevent that error prone table from reflecting?

来源:https://stackoverflow.com/questions/64680802/column-does-not-exist-error-on-createtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!