简体   繁体   English

如何使用 SQLAlchemy 为 Aurora Serverless 和 DataAPI 生成 python db model?

[英]How to generate python db model with SQLAlchemy for Aurora Serverless and DataAPI?

I've been trying to figure out if there's any way to have ORM functionalities (especially code model generation) for an Aurora Serverless (Postgresql) DB that's used through DataAPI?我一直在试图弄清楚是否有任何方法可以为通过 DataAPI 使用的 Aurora Serverless (Postgresql) DB 提供 ORM 功能(尤其是代码 model 生成)? Ultimately, I want to avoid using raw sql string queries in our Lambdas.最终,我想避免在我们的 Lambda 中使用原始 sql 字符串查询。

I tried using sqlacodegen combined with sqlalchemy-aurora-data-api (which works on top of sqlalchemy ) but I keep getting errors:我尝试将sqlacodegen与 sqlalchemy -aurora-data-api (在sqlalchemy之上工作)结合使用,但我不断收到错误消息:

With dialect:用方言:

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

botocore.exceptions.NoRegionError: You must specify a region. botocore.exceptions.NoRegionError:您必须指定一个区域。

Without dialect:没有方言:

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

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection timed out (0x0000274C/10060) sqlalchemy.exc.OperationalError:(psycopg2.OperationalError)无法连接到服务器:连接超时(0x0000274C/10060)

The former seems to hint that parameters must be passed to the call, but sqlacodegen doesn't take any kwargs afaik.前者似乎暗示必须将参数传递给调用,但 sqlacodegen 不接受任何 kwargs afaik。 The latter just fails to connect and the psycopg2 tells me it's just not using the proper dialect anyway.后者只是无法连接,而psycopg2告诉我它只是没有使用正确的方言。

None of these two calls are correct.这两个调用都不正确。 Here is the right way to call it:这是调用它的正确方法:

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

Two main points here:这里主要有两点:

  1. The error indicating You must specify a region is correct.提示You must specify a region的错误是正确的。 The way to specify that region when using command line scripts (such as sqlacodegen ) is to use Environment Variables .使用命令行脚本(例如sqlacodegen )时指定该区域的方法是使用Environment Variables This section of the boto3 documentation mentions all the variables and their usage. boto3文档的这一部分提到了所有变量及其用法。

  2. The specific library ( sqlalchemy-aurora-data-api ) used for DataAPI uses such a format in their example, and it works fine as is despite being mixed with sqlacodegen .用于 DataAPI 的特定库( sqlalchemy-aurora-data-api )在他们的示例中使用了这种格式,尽管与sqlacodegen混合使用,它仍然可以正常工作。

For this problem, the AWS_DEFAULT_REGION variable should be set to the appropriate value (ie us-east-1 ), along with all the necessary AWS credentials (like AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY ).对于这个问题, AWS_DEFAULT_REGION变量设置为适当的值(即us-east-1 )以及所有必要的 AWS 凭证(如AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 使用 SQLAlchemy 连接到 AWS Aurora Serverless MySQL - Python Connect to AWS Aurora Serverless MySQL Using SQLAlchemy 使用 DB 数据模型生成 SQLAlchemy 模型、模式和 JSON 响应 - Use DB data model to generate SQLAlchemy models, schemas, and JSON response Python:AWS Aurora 无服务器数据 API:用户密码验证失败 - Python: AWS Aurora Serverless Data API: password authentication failed for user 如何在SQLAlchemy(Python,Flask)数据库模型的构造函数中设置外键? - How do I set a foreign key in the constructor of a SQLAlchemy (Python, Flask) db model? 使用SQLAlchemy(Python)创建模型对象时生成GUID - Generate GUID when creating model object using SQLAlchemy (Python) 通过python中的rds创建aurora数据库实例 - Create aurora db instance via rds in python Python with sqlalchemy db retrival - Python with sqlalchemy db retrival 在SQLAlchemy模型中查询数据库 - Querying DB inside SQLAlchemy Model 如何将默认的`db.Model`子类化为用SQLAlchemy作为自定义`Base` - How to subclass the default `db.Model` to use as a custom `Base` with SQLAlchemy 如何在 DB (MYSQL) 中保留 VARCHAR,但在 sqlalchemy model 中保留 ENUM - How to keep VARCHAR in the DB (MYSQL), but ENUM in the sqlalchemy model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM