SQLAlchemy 未找到与 postgres_fdw 相关联的 Postgres table
SQLAlchemy not finding Postgres table connected with postgres_fdw
请原谅任何术语拼写错误,除了 SQLite 之外,没有太多使用数据库的经验。我正在尝试复制我将在 SQLite 中执行的操作,在那里我可以将一个数据库附加到第二个数据库并查询所有 tables。我没有将 SQLAlchemy 与 SQLite
一起使用
我在 Win7/54 上使用 SQLAlchemy 1.0.13、Postgres 9.5 和 Python 3.5.2(使用 Anaconda)。我已经使用 postgres_fdw 连接了两个数据库(在本地主机上),并从辅助数据库导入了一些 table。我可以在 PgAdminIII 中使用 SQL 和使用 psycopg2 从 Python 成功地手动查询连接的 table。我试过 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())
和附加的 table 未列出(来自主数据库的 table 显示)。有什么方法可以完成我想要完成的事情吗?
为了在Table
实例中映射一个tableSQLAlchemy needs there to be at least one column denoted as a primary key column. This does not mean that the column need actually be a primary key column in the eyes of the database, though it is a good idea. Depending on how you've imported the table from your foreign schema it may not have a representation of a primary key constraint, or any other constraints for that matter. You can work around this by either overriding the reflected primary key column(不在映射的类主体中) ,或者更好的是告诉映射器哪些列包含候选键:
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
}
要检查外国 table 名称,请使用 PGInspector.get_foreign_table_names()
方法:
print(insp.get_foreign_table_names())
请原谅任何术语拼写错误,除了 SQLite 之外,没有太多使用数据库的经验。我正在尝试复制我将在 SQLite 中执行的操作,在那里我可以将一个数据库附加到第二个数据库并查询所有 tables。我没有将 SQLAlchemy 与 SQLite
一起使用我在 Win7/54 上使用 SQLAlchemy 1.0.13、Postgres 9.5 和 Python 3.5.2(使用 Anaconda)。我已经使用 postgres_fdw 连接了两个数据库(在本地主机上),并从辅助数据库导入了一些 table。我可以在 PgAdminIII 中使用 SQL 和使用 psycopg2 从 Python 成功地手动查询连接的 table。我试过 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())
和附加的 table 未列出(来自主数据库的 table 显示)。有什么方法可以完成我想要完成的事情吗?
为了在Table
实例中映射一个tableSQLAlchemy needs there to be at least one column denoted as a primary key column. This does not mean that the column need actually be a primary key column in the eyes of the database, though it is a good idea. Depending on how you've imported the table from your foreign schema it may not have a representation of a primary key constraint, or any other constraints for that matter. You can work around this by either overriding the reflected primary key column(不在映射的类主体中) ,或者更好的是告诉映射器哪些列包含候选键:
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
}
要检查外国 table 名称,请使用 PGInspector.get_foreign_table_names()
方法:
print(insp.get_foreign_table_names())