带有postgresql的SQLAlchemy产生错误“找不到列'attype'的行

时间:2014-03-04 00:09:09

标签: python postgresql sqlalchemy

我正在使用SQLAlchemy连接到postgresql数据库。我这样做:

>>> my_engine = create_engine("postgresql://username@localhost:5432/dbname")
>>> my_metadata = MetaData(bind = my_engine)

(数据库上没有密码。)这些没有任何问题,但后来我无法反映这些表:

>>> my_metadata.reflect()

并收到错误:

sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column 'attype'"

造成这种情况的原因是什么?如何解决?它看起来像架构级别的东西,但我无法弄明白。我可以连接到MySQL数据库并反映表没有任何问题。

编辑:SqlAlchemy 0.9.2,psql server 9.2.5,psql client 8.4.13,psycopg2 2.5.1

编辑:使用echo = True

INFO:root:Creating music_engine postgresql://postgres@localhost/musicbrainz_db
INFO:sqlalchemy.engine.base.Engine:select version()
INFO:sqlalchemy.engine.base.Engine:{}
INFO:sqlalchemy.engine.base.Engine:select current_schema()
INFO:sqlalchemy.engine.base.Engine:{}
INFO:sqlalchemy.engine.base.Engine:SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1
INFO:sqlalchemy.engine.base.Engine:{}
INFO:sqlalchemy.engine.base.Engine:SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1
INFO:sqlalchemy.engine.base.Engine:{}
INFO:sqlalchemy.engine.base.Engine:show standard_conforming_strings
INFO:sqlalchemy.engine.base.Engine:{}
INFO:sqlalchemy.engine.base.Engine:SELECT relname FROM pg_class c WHERE relkind = 'r' AND 'echonest' = (select nspname from pg_namespace n where n.oid = c.re
lnamespace)
INFO:sqlalchemy.engine.base.Engine:{}
INFO:sqlalchemy.engine.base.Engine:
            SELECT c.oid
            FROM pg_catalog.pg_class c
            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE (pg_catalog.pg_table_is_visible(c.oid))
            AND c.relname = %(table_name)s AND c.relkind in ('r','v')

INFO:sqlalchemy.engine.base.Engine:{'table_name': u'status'}
INFO:sqlalchemy.engine.base.Engine:
            SELECT a.attname,
              pg_catalog.format_type(a.atttypid, a.atttypmod),
              (SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid)
                FROM pg_catalog.pg_attrdef d
               WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum
               AND a.atthasdef)
              AS DEFAULT,
              a.attnotnull, a.attnum, a.attrelid as table_oid
            FROM pg_catalog.pg_attribute a
            WHERE a.attrelid = %(table_oid)s
            AND a.attnum > 0 AND NOT a.attisdropped
            ORDER BY a.attnum

INFO:sqlalchemy.engine.base.Engine:{'table_oid': 19271}
INFO:sqlalchemy.engine.base.Engine:
            SELECT t.typname as "name",
               pg_catalog.format_type(t.typbasetype, t.typtypmod) as "attype",
               not t.typnotnull as "nullable",
               t.typdefault as "default",
               pg_catalog.pg_type_is_visible(t.oid) as "visible",
               n.nspname as "schema"
            FROM pg_catalog.pg_type t
               LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
            WHERE t.typtype = 'd'

INFO:sqlalchemy.engine.base.Engine:{}

1 个答案:

答案 0 :(得分:0)

问题确实与版本有关,但在Python和psycopg2中,而不是SQLAlchemy或PostgresQL。

我在python2.6站点包中安装了SQLAlchemy。我适当地设置了PYTHONPATH,所以我认为无论是使用python2.6还是python2.7都没关系。但是,它很重要因为SQLAlchemy总是为python2.6调用psycopg2,而为python2.6编译的psycopg2的本机部分与python2.7不兼容。