Flask Rest API SQL Alchemy 连接 Cloud SQL PostgreSQL

huangapple go评论64阅读模式
英文:

Flask Rest Api SQL Alchemy connection Cloud Sql Postgresq

问题

你的问题是Flask应用在调用与SQLAlchemy相关的端点时出现错误,错误信息是: "The current Flask app is not registered with this 'SQLAlchemy'"。这通常是因为没有正确初始化SQLAlchemy或者创建了多个SQLAlchemy实例导致的。

要解决这个问题,你可以尝试以下步骤:

  1. 在app.py中初始化SQLAlchemy:确保在app.py中正确初始化SQLAlchemy,可以使用db.init_app(app)来注册Flask应用。
from flask_sqlalchemy import SQLAlchemy

# ...

app = Flask(__name)

# Initialize SQLAlchemy with your app
db = SQLAlchemy(app)
  1. 删除全局的db变量:在app.py中,删除全局的db变量,因为你已经将SQLAlchemy实例与Flask应用关联,不再需要全局db变量。
db = None
  1. 修改init_connection_pool函数:确保init_connection_pool函数正确获取Flask应用的Unix socket路径。你可以使用app.config来获取应用配置。
def init_connection_pool() -> sqlalchemy.engine.base.Engine:
    # use a Unix socket when INSTANCE_UNIX_SOCKET (e.g. /cloudsql/project:region:instance) is defined
    unix_socket_path = app.config.get("INSTANCE_UNIX_SOCKET")
    if unix_socket_path:
        return connect_unix_socket()
    raise ValueError("Missing database connection type. Please define one of INSTANCE_HOST, INSTANCE_UNIX_SOCKET, or INSTANCE_CONNECTION_NAME")
  1. 更新User.py文件:确保User.py文件中没有创建额外的SQLAlchemy实例。只使用Flask应用中已经初始化的SQLAlchemy实例。

这些更改应该有助于解决你的问题,确保Flask应用和SQLAlchemy正确集成并使用相同的实例。如果问题仍然存在,请确保你的依赖包已正确安装,并且数据库连接配置正确。

英文:

I have a connection problem with Cloud Sql Postgres from my Flask Rest API app.
I have a db.py file:

import os
from flask_sqlalchemy import SQLAlchemy

import sqlalchemy

db = SQLAlchemy()


def connect_unix_socket() -> sqlalchemy.engine.base.Engine:
""" Initializes a Unix socket connection pool for a Cloud SQL instance of Postgres. """
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
# keep secrets safe.
db_user = os.environ["DB_USER"]  # e.g. 'my-database-user'
db_pass = os.environ["DB_PASS"]  # e.g. 'my-database-password'
db_name = os.environ["DB_NAME"]  # e.g. 'my-database'
unix_socket_path = os.environ["INSTANCE_UNIX_SOCKET"]  # e.g. '/cloudsql/project:region:instance'


pool = sqlalchemy.create_engine(
    # Equivalent URL:
    # postgresql+pg8000://<db_user>:<db_pass>@/<db_name>
    #                         ?unix_sock=<INSTANCE_UNIX_SOCKET>/.s.PGSQL.5432
    # Note: Some drivers require the `unix_sock` query parameter to use a different key.
    # For example, 'psycopg2' uses the path set to `host` in order to connect successfully.
    sqlalchemy.engine.url.URL.create(
        drivername="postgresql+pg8000",
        username=db_user,
        password=db_pass,
        database=db_name,
        query={"unix_sock": "{}/.s.PGSQL.5432".format(unix_socket_path)},
    ),
    # [START_EXCLUDE]
    # Pool size is the maximum number of permanent connections to keep.
    pool_size=5,

    # Temporarily exceeds the set pool_size if no connections are available.
    max_overflow=2,

    # The total number of concurrent connections for your application will be
    # a total of pool_size and max_overflow.

    # 'pool_timeout' is the maximum number of seconds to wait when retrieving a
    # new connection from the pool. After the specified amount of time, an
    # exception will be thrown.
    pool_timeout=30,  # 30 seconds

    # 'pool_recycle' is the maximum number of seconds a connection can persist.
    # Connections that live longer than the specified amount of time will be
    # re-established
    pool_recycle=1800,  # 30 minutes
    # [END_EXCLUDE]
)
return pool

I import the db.py file in my app.py file:

import os
import sqlalchemy

from flask import Flask
from flask_smorest import Api
from flask_sqlalchemy import SQLAlchemy

from db import db, connect_unix_socket
import models

from resources.user import blp as UserBlueprint

# pylint: disable=C0103
app = Flask(__name__)

def init_connection_pool() -> sqlalchemy.engine.base.Engine:

    # use a Unix socket when INSTANCE_UNIX_SOCKET (e.g. /cloudsql/project:region:instance) is defined
    if unix_socket_path:
        return connect_unix_socket()
    raise ValueError(
        "Missing database connection type. Please define one of INSTANCE_HOST, INSTANCE_UNIX_SOCKET, or INSTANCE_CONNECTION_NAME"
    )

db = None

@app.before_first_request
def init_db() -> sqlalchemy.engine.base.Engine:
    global db
    db = init_connection_pool()


    
api = Api(app)

@app.route("/api")
def user_route():
    return "Welcome user API!"


api.register_blueprint(UserBlueprint)

if __name__ == '__main__':
    server_port = os.environ.get('PORT', '8080')
    app.run(debug=True, port=server_port, host='0.0.0.0')

The app run correctly, when i call the end point to Get or Post users, the app crash and give me this error:

"The current Flask app is not registered with this 'SQLAlchemy'"
RuntimeError: The current Flask app is not registered with this 'SQLAlchemy' instance. Did you forget to call 'init_app', or did you create multiple 'SQLAlchemy' instances?

This is my User.py class:

from sqlalchemy.exc import SQLAlchemyError, IntegrityError

from db import db
from models import UserModel
from schemas import UserSchema


blp = Blueprint("Users", "users", description="Operations on users")

@blp.route("/user/<string:user_id>")
class User(MethodView):
    @blp.response(200, UserSchema)
    def get(self, user_id):
        user = UserModel.query.get_or_404(user_id)
        return user

    def delete(self, user_id):
        user = UserModel.query.get_or_404(user_id)
        db.session.delete(user)
        db.session.commit()
        return {"message": "User deleted"}, 200


@blp.route("/user")
class UserList(MethodView):
    @blp.response(200, UserSchema(many=True))
    def get(self):
        return UserModel.query.all()

How i can fix this issue?

答案1

得分: 1

@dev_ 你的问题在于你试图混合使用SQLAlchemy CoreSQLAlchemy ORM,好像它们是一样的,这导致了你的问题。使用 sqlalchemy.create_engine 创建的 SQLAlchemy 连接池使用 CORE API,而 Flask-SQLAlchemy 使用了 SQLAlchemy ORM 模型。这是导致你问题的核心原因。最好只使用其中一个。

我建议你纯粹地使用 Flask-SQLALchemy,并使用 cloud-sql-python-connector 库来满足你的用例。这将使你的生活更加轻松。

为了简化,我将去掉你的 db.py,让你的 app.py 文件如下:

from flask import Flask
from flask_smorest import Api
from flask_sqlalchemy import SQLAlchemy
from google.cloud.sql.connector import Connector, IPTypes

from resources.user import blp as UserBlueprint

# 加载环境变量
db_user = os.environ["DB_USER"]  # 例如,'my-database-user'
db_pass = os.environ["DB_PASS"]  # 例如,'my-database-password'
db_name = os.environ["DB_NAME"]  # 例如,'my-database'
instance_connection_name = os.environ["INSTANCE_CONNECTION_NAME"]  # 例如,'project:region:instance'

# Python Connector 数据库连接函数
def getconn():
    with Connector() as connector:
        conn = connector.connect(
            instance_connection_name,  # Cloud SQL 实例连接名称
            "pg8000",
            user=db_user,
            password=db_pass,
            db=db_name,
            ip_type= IPTypes.PUBLIC  # IPTypes.PRIVATE 用于私有 IP
        )
        return conn

app = Flask(__name__)

# 配置 Flask-SQLAlchemy 使用 Python Connector
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql+pg8000://"
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
    "creator": getconn
}
# 初始化数据库(使用 app!)
db = SQLAlchemy(app)

# 你的代码的其余部分
api = Api(app)

# ...

希望这有助于解决你的问题!

英文:

@dev_ Your issue is that your are trying to intermingle the use of SQLAlchemy Core with SQLAlchemy ORM as if they are the same thing, leading to your issues. SQLAlchemy connection pools created using sqlalchemy.create_engine use the CORE API while Flask-SQLAlchemy uses the SQLAlchemy ORM model. This is the core reason for you issue. It is easier to use one or the other.

I would recommend using purely Flask-SQLALchemy with the use of the cloud-sql-python-connector library for your use-case. It will make your life much easier.

For simplicity, I am getting rid of your db.py leading to your app.py file being as follows:

from flask import Flask
from flask_smorest import Api
from flask_sqlalchemy import SQLAlchemy
from google.cloud.sql.connector import Connector, IPTypes

from resources.user import blp as UserBlueprint

# load env vars
db_user = os.environ["DB_USER"]  # e.g. 'my-database-user'
db_pass = os.environ["DB_PASS"]  # e.g. 'my-database-password'
db_name = os.environ["DB_NAME"]  # e.g. 'my-database'
instance_connection_name = os.environ["INSTANCE_CONNECTION_NAME"]  # e.g. 'project:region:instance'

# Python Connector database connection function
def getconn():
    with Connector() as connector:
        conn = connector.connect(
            instance_connection_name, # Cloud SQL Instance Connection Name
            "pg8000",
            user=db_user,
            password=db_pass,
            db=db_name,
            ip_type= IPTypes.PUBLIC  # IPTypes.PRIVATE for private IP
        )
        return conn


app = Flask(__name__)

# configure Flask-SQLAlchemy to use Python Connector
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql+pg8000://"
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
    "creator": getconn
}
# initialize db (using app!)
db = SQLAlchemy(app)

# rest of your code    
api = Api(app)

# ...

Hope this helps resolve your issue!

huangapple
  • 本文由 发表于 2023年2月14日 19:28:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定