I want check whether the particular email template is used in any other model instance and model Rule we have action(JSONB) while running query below query i’m getting error
I had added the sample query and error response
Sample response structure of action column
{
"negative": [],
"positive": [
{
"action": 45,
"email_template_id": 1
}
]
}
Query to check email_template
event_exists_subquery = exists().where(Event.email_template_id == EmailTemplate.id)
subquery = exists().where(
or_(
func.jsonb_contains(
Rule.actions["positive"],
func.jsonb(literal({"email_template_id": EmailTemplate.id})),
),
func.jsonb_contains(
Rule.actions["negative"],
func.jsonb(literal({"email_template_id": EmailTemplate.id})),
),
)
)
statement = select(
EmailTemplate.id,
EmailTemplate.name,
case((or_(event_exists_subquery, subquery), False), else_=True).label("is_used"),
)
email_templates = (await request.state.db.execute(statement)).mappings().all()
Sample Error response
sqlalchemy.exc.ProgrammingError: (psycopg.ProgrammingError) cannot adapt type 'dict' using placeholder '%s' (format: AUTO)
[SQL: SELECT email_templates.id, email_templates.name, CASE WHEN (EXISTS (SELECT *
FROM events
WHERE events.email_template_id = email_templates.id)) THEN %(param_1)s ELSE %(param_2)s END AS is_editable, CASE WHEN (EXISTS (SELECT *
FROM rules
WHERE jsonb_contains((rules.actions -> %(actions_1)s::TEXT), jsonb(%(param_3)s)) OR jsonb_contains((rules.actions -> %(actions_2)s::TEXT), jsonb(%(param_4)s)))) THEN %(param_5)s ELSE %(param_6)s END AS is_used
FROM email_templates
WHERE email_templates.deleted_at IS NULL]
[parameters: {'param_1': False, 'param_2': True, 'actions_1': 'positive', 'param_3': {'email_template_id': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x115d52ca0>}, 'actions_2': 'negative', 'param_4': {'email_template_id': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x115d52ca0>}, 'param_5': False, 'param_6': True}]
I had below packages:
- python = “^3.11”
- fastapi = “^0.111.0”
- sqlalchemy = “^2.0.28”
- psycopg = “^3.2.1”
- psycopg-binary = “^3.2.1”
DB: Postgres
I had tried the below approach also to resolve the same but getting same type of error
subquery = exists().where(
or_(
Rule.actions["positive"].contains(
[{"email_template_id": email_template_id}]
),
Rule.actions["negative"].contains(
[{"email_template_id": email_template_id}]
),
)
)