Using postgres row_to_json
as a Projection
type
#447
Replies: 2 comments 1 reply
-
Update: A raw expression works fine Assignments.
INSERT(Assignments.AllColumns).
MODEL(&input).
RETURNING(
Assignments.AllColumns,
Projects.
SELECT(jet.Raw("row_to_json(projects.*)")).
WHERE(Projects.ID.EQ(Assignments.ProjectID)).
AS("project"),
) I also made a utility to do this in a naive way: func ROW_TO_JSON(table jet.Table) jet.Expression {
return jet.Raw(fmt.Sprintf("row_to_json(%s.*)", table.TableName()))
} However, I think it could be improved using a Assignments.
INSERT(Assignments.AllColumns).
MODEL(&input).
RETURNING(
Assignments.AllColumns,
Projects.
SELECT(
pg.Raw("row_to_json(t)"),
).
FROM(
Projects.
SELECT(Projects.AllColumns.Except(Projects.DeletedAt)).
WHERE(Projects.ID.EQ(Assignments.ProjectID)).
AsTable("t"),
).
AS("project"),
) The issue with the above is that the SQL serializer automatically appends aliases to every column in the projection, which muddles the json: INSERT INTO public.assignments (...)
VALUES (...)
RETURNING assignments.id AS "assignments.id",
(
SELECT row_to_json(t)
FROM (
SELECT projects.id AS "projects.id",
projects.name AS "projects.name",
projects.created_at AS "projects.created_at",
projects.updated_at AS "projects.updated_at",
projects.deleted_at AS "projects.deleted_at"
FROM public.projects
WHERE projects.id = assignments.project_id
) AS t
) AS "project"; Result: {
"projects.id": "2s6UISqaxZlEdkuKxh4EsIOvi5P",
"projects.name": "Test Project",
"projects.created_at": "2025-01-25T03:23:57.848449+00:00",
"projects.updated_at": null,
"projects.deleted_at": null
} |
Beta Was this translation helpful? Give feedback.
-
Is JSON a requirement or a way to return all stmt := WITH(
insertedAssigment.AS(
Assignments.INSERT(Assignments.AllColumns).
MODEL(&input).
RETURNING(Assignments.AllColumns)
),
)(
SELECT(
insertedAssigment.AllColumns(),
Projects.AllColumns,
).FROM(
insertedAssigment.LEFT_JOIN(Projects, ....
)
) This way you can scan directly using qrm: var dest struct {
model.Assigment
Projects []model.Projects
}
err := stmt.Query(db, &dest) |
Beta Was this translation helpful? Give feedback.
-
Hi! I'd like to use the postgres
row_to_json
function with jet. My end goal is to convert a row selected via subquery to a single json column that can then be unmarshaled later.The serialized query would look something like:
I'm not sure how to do this with jet or if its even supported. I would expect this syntax to look something like:
A modifier on the
ColumnList
typeOr a wrapper
I did notice there is a
pg.Func
helper that seems to do something like this but this produces invalid syntax, where the row_to_json function wraps the entire expression instead of the projection.This is an invalid query since a subquery can only return a single column
It looks like we would need a similar helper for the
ColumnList
type.If this isn't implemented I'd be happy to put something together with a little guidance!
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions