【发布时间】:2021-11-03 23:35:18
【问题描述】:
在我的postgres 服务器中,我们有一个数据库database 有2 个模式:public 和api。
public 有几个表,我需要在api 中创建一个表,并使用public 中名为model 的表的外键。
所以它是:
-Schemas
--public
---tables
----models
--api
---tables
使用 SQLAlchemy 我有以下课程:
from sqlalchemy import create_engine, MetaData, Table, Column
class __PostgresService:
def __init__(self):
self.__client = create_engine("postgresql://postgres@localhost:5432/database")
metadata = MetaData(self.__client, schema="public")
self.__table = Table("training", metadata,
Column("id", String, primary_key=True, nullable=False),
Column("model_id", ForeignKey("model.id"), nullable=False),
schema="api")
metadata.create_all()
postgres_service = __PostgresService()
但是在启动时我收到以下错误:
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'training.model_id' could not find table 'public.model' with which to generate a foreign key to target column 'id'
它似乎确实在寻找正确的东西但找不到它?我对为什么会发生这种情况感到非常困惑,特别是因为错误是指找不到“public”,这是由postgres 默认创建的,而不是我在 pgAdmin 中创建的“api”。
我是否缺少一些关键配置?
【问题讨论】:
标签: python postgresql sqlalchemy