使用SQLmodel连接Serverless CockroachDB时遇缺少集群标识符错误
问题:SQLModel连接CockroachDB提示缺少集群标识符错误
背景
我开发的Python应用使用SQLModel作为ORM,之前基于SQLite3可以正常创建数据库及表,现在需要切换到CockroachDB。已按照官方文档修改create_engine的连接字符串,且明确SQLModel完全兼容SQLAlchemy连接方式。
报错详情
运行以下代码:
engine = create_engine(os.environ["DB_CONNECTION"], echo=True)
在执行SQLModel.metadata.create_all(engine)时触发错误:
Exception has occurred: OperationalError (psycopg2.OperationalError) FATAL: codeParamsRoutingFailed: missing cluster identifier (Background on this error at: https://sqlalche.me/e/14/e3q8) psycopg2.OperationalError: FATAL: codeParamsRoutingFailed: missing cluster identifier The above exception was the direct cause of the following exception: File "/workspace/app/main.py", line 8, in <module> SQLModel.metadata.create_all(engine) sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: codeParamsRoutingFailed: missing cluster identifier (Background on this error at: https://sqlalche.me/e/14/e3q8)
当前配置信息
- 环境变量设置(VSCode的devcontainer.json):
"containerEnv": { "DB_CONNECTION": "cockroachdb://wg:<removed-password>@cheeky-panther-6202.8nj.cockroachlabs.cloud:26257/wartgeld?sslmode=verify-full" }
- 证书下载命令:
curl --create-dirs -o $HOME/.postgresql/root.crt -O https://cockroachlabs.cloud/clusters/47771648-29f6-4b5d-b684-aeb905732cd7/cert
- 已安装依赖包:
psycopg2-binary 2.9.5 pydantic 1.10.2 PyYAML 6.0 requests 2.28.1 setuptools 65.5.0 SQLAlchemy 1.4.41 sqlalchemy-cockroachdb 1.4.4 sqlalchemy2-stubs 0.0.2a29 sqlmodel 0.0.8 typing_extensions 4.4.0 urllib3 1.26.13 uvicorn 0.20.0 wheel 0.38.4
疑问
连接字符串是直接从CockroachDB管理界面复制而来,但仍不确定是否正确,希望解决该连接错误。
内容的提问来源于stack exchange,提问作者comboner
相关产品推荐
相关产品推荐

