Skip to content

[🐛 Bug]: SQLAlchemy UndefinedFunction in get_alerts_by_fingerprint #5357

@diegovb-sys

Description

@diegovb-sys

Describe the bug
Using Postgres as db engine, and calling get_alerts_by_fingerprint with a status != None.

if status:
            query = query.filter(func.json_extract(Alert.event, "$.status") == status)

I get the following error message:

File \"/venv/lib/python3.13/site-packages/sqlalchemy/engine/default.py\", line 941, in do_execute\n cursor.execute(statement, parameters)\n ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^\npsycopg2.errors.UndefinedFunction: function json_extract(json, unknown) does not exist\

File \"/venv/lib/python3.13/site-packages/sqlalchemy/engine/default.py\", line 941, in do_execute\n cursor.execute(statement, parameters)\n ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^\nsqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedFunction) function json_extract(json, unknown) does not exist\nLINE 3: ...f8cc44d184b341be7a46e7fc11f6c065a59545cd0d84' AND json_extra...\n ^\nHINT: No function matches the given name and argument types. You might need to add explicit type casts.

Therefore, this call is not agnostic with respect to the database engine.

Additional context
In order to fix it, it should be swap by:

def get_json_extract_field(session, base_field, key):
    if session.bind.dialect.name == "postgresql":
        return func.json_extract_path_text(base_field, key)
    elif session.bind.dialect.name == "mysql":
        return func.json_unquote(func.json_extract(base_field, "$.{}".format(key)))
    else:
        return func.json_extract(base_field, "$.{}".format(key))

Moreover, it must be taking into account that this behavior is not isolate. There are a few of functions with the same dependency of the db engine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions