【问题标题】:How to generate python db model with SQLAlchemy for Aurora Serverless and DataAPI?如何使用 SQLAlchemy 为 Aurora Serverless 和 DataAPI 生成 python 数据库模型?
【发布时间】:2021-03-27 12:33:17
【问题描述】:

我一直试图弄清楚是否有任何方法可以为通过 DataAPI 使用的 Aurora Serverless (Postgresql) DB 提供 ORM 功能(尤其是代码模型生成)?最终,我想避免在我们的 Lambda 中使用原始 sql 字符串查询。

我尝试将sqlacodegensqlalchemy-aurora-data-api 结合使用(在sqlalchemy 之上工作),但我不断收到错误:

有方言:

> sqlacodegen postgresql+auroradataapi://username:password@db-host/db-name

botocore.exceptions.NoRegionError: 你必须指定一个区域。

没有方言:

> sqlacodegen postgresql://username:password@db-host/db-name

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) 无法连接到服务器:连接超时 (0x0000274C/10060)

前者似乎暗示必须将参数传递给调用,但 sqlacodegen 不接受任何 kwargs afaik。后者只是无法连接,psycopg2 告诉我它只是没有使用正确的方言。

【问题讨论】:

    标签: python postgresql orm sqlalchemy aws-aurora-serverless


    【解决方案1】:

    这两个调用都不正确。这是正确的称呼方式:

    > sqlacodegen postgresql+auroradataapi://:@/db-name
    

    这里有两个要点:

    1. 表示You must specify a region的错误是正确的。使用命令行脚本(例如sqlacodegen)时指定该区域的方法是使用环境变量boto3 文档的This section 提到了所有变量及其用法。

    2. 用于 DataAPI 的特定库 (sqlalchemy-aurora-data-api) 在他们的示例中使用了这种格式,尽管与 sqlacodegen 混合使用,它仍然可以正常工作。 p>

    对于这个问题,应将AWS_DEFAULT_REGION 变量设置为适当的值(即us-east-1),以及所有必要的AWS 凭证(如AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2020-10-16
      • 2017-05-21
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多