postgresql JSON(B) dialect: update value of a specific key in a JSON object

21 views
Skip to first unread message

Massimiliano della Rovere

unread,
Oct 16, 2020, 4:01:27 AM10/16/20
to sqlal...@googlegroups.com
Greetings,
how should I write the .values() section of a CORE update() statement to render the following
postgres syntax?

The data column is a JSON(B) and contains a dict object.

UPDATE settings
SET data = data || '{"key": "value"}'
WHERE key = 'my_param';

Thanks,
Massimiliano

Mike Bayer

unread,
Oct 16, 2020, 8:20:19 AM10/16/20
to noreply-spamdigest via sqlalchemy
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import update
from sqlalchemy.dialects import postgresql
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)
    data = Column(JSONB)

stmt = update(A).values({A.data: A.data + {"key": "value"}}).where(A.id == 5)

print(stmt.compile(dialect=postgresql.dialect()))
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages