2

PostgreSQL has functions like jsonb_to_record or jsonb_populate_record which allow you convert data stored in a JSONB column to separate columns. however, I'm using SQLAlchemy and jsonb_to_record requires a predefined structure for the output (see example below from the PostgreSQL docs)

select * from json_to_record('{"a":1,"b":[1,2,3],"c":"bar"}') as x(a int, b text, d text)

Is there a way to use these functions from SQLAlchemy?

3
  • By SQLAlchemy, are we talking about the ORM or the core part? Commented Feb 28, 2020 at 21:29
  • both, if possible, please. Commented Feb 29, 2020 at 1:40
  • I think this answer should help stackoverflow.com/a/25046952/4454340 Commented May 27, 2020 at 13:04

1 Answer 1

2

you can use any postgres function in SQLAlchemy with func like so:

from SQLAlchemy import func
session.query(func.json_to_record(Model.jsonb_field))

Do note that set-returning functions are a bit of a pain to deal with with SQLAlchemy, but if you want you can get it to work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.