Python SQLAlchemy未找到与Postgres_fdw连接的Postgres表

Python SQLAlchemy未找到与Postgres_fdw连接的Postgres表,python,postgresql,sqlalchemy,postgres-fdw,Python,Postgresql,Sqlalchemy,Postgres Fdw,请原谅任何术语拼写错误,除了SQLite之外,我对数据库没有太多经验。我正在尝试复制我在SQLite中所做的工作,在SQLite中,我可以将一个数据库附加到另一个数据库,并跨所有表进行查询。我没有在SQLite中使用SQLAlchemy 我正在Win7/54上使用SQLAlchemy 1.0.13、Postgres 9.5和Python 3.5.2(使用Anaconda)。我使用postgres_fdw连接了两个数据库(在本地主机上),并从辅助数据库导入了一些表。我可以使用PgAdminIII中

请原谅任何术语拼写错误,除了SQLite之外,我对数据库没有太多经验。我正在尝试复制我在SQLite中所做的工作,在SQLite中,我可以将一个数据库附加到另一个数据库,并跨所有表进行查询。我没有在SQLite中使用SQLAlchemy

我正在Win7/54上使用SQLAlchemy 1.0.13、Postgres 9.5和Python 3.5.2(使用Anaconda)。我使用postgres_fdw连接了两个数据库(在本地主机上),并从辅助数据库导入了一些表。我可以使用PgAdminIII中的SQL和使用psycopg2的Python成功地手动查询连接的表。使用SQLAlchemy,我尝试过:

# Same connection string info that psycopg2 used
engine = create_engine(conn_str, echo=True)

class TestTable(Base):
    __table__ = Table('test_table', Base.metadata,
                      autoload=True, autoload_with=engine)

    # Added this when I got the error the first time
    # test_id is a primary key in the secondary table
    Column('test_id', Integer, primary_key=True)
并获取错误:

sqlalchemy.exc.ArgumentError: Mapper Mapper|TestTable|test_table could not
assemble any primary key columns for mapped table 'test_table'
然后我试着:

insp = reflection.Inspector.from_engine(engine)
print(insp.get_table_names())
并且未列出附加表(主数据库中的表会显示)。有什么方法可以完成我想要完成的任务吗?

以便映射表。这并不意味着该列实际上需要成为数据库眼中的主键列,尽管这是一个好主意。根据您从外部模式导入表的方式,表可能没有主键约束或任何其他约束的表示形式。您可以在
实例
(不在mapped classes主体中)中解决此问题,或者更好地告诉映射器哪些列构成候选键:

engine = create_engine(conn_str, echo=True)

test_table = Table('test_table', Base.metadata,
                   autoload=True, autoload_with=engine)

class TestTable(Base):
    __table__ = test_table
    __mapper_args__ = {
        'primary_key': (test_table.c.test_id, )  # candidate key columns
    }
要检查外来表名,请使用以下方法:

以便映射表。这并不意味着该列实际上需要成为数据库眼中的主键列,尽管这是一个好主意。根据您从外部模式导入表的方式,表可能没有主键约束或任何其他约束的表示形式。您可以在
实例
(不在mapped classes主体中)中解决此问题,或者更好地告诉映射器哪些列构成候选键:

engine = create_engine(conn_str, echo=True)

test_table = Table('test_table', Base.metadata,
                   autoload=True, autoload_with=engine)

class TestTable(Base):
    __table__ = test_table
    __mapper_args__ = {
        'primary_key': (test_table.c.test_id, )  # candidate key columns
    }
要检查外来表名,请使用以下方法: