Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

add macro sqlc.cast to cast types only in the generated code struct, but not in the generated query #4081

Open
@khalidibnwalid

Description

What do you want to change?

Problem

When I need to use json_agg of PostgreSQL, it will be casted to an interface{}, so I tried to cast it to a custom struct type

-- -- name: GetRoomsGroupWithRooms :many
SELECT 
 sqlc.embed(rooms_groups),
 COALESCE(
 json_agg(rooms) FILTER (WHERE rooms.id IS NOT NULL),
 '[]'
 )::room[] AS rooms

I then added

 overrides:
 - db_type: "room"
 go_type:
 import: "github.com/me/mypkg/models"
 type: "Rooms"

It did generate the wanted struct

type GetRoomsGroupWithRooms struct {
	RoomsGroup RoomsGroup `json:"rooms_group"`
	Rooms []models.Rooms `json:"rooms"` // generated the proper type instead of []interface{}
}

But it did also include the ::room[] part in the generated query, which will cause an error in postgres

Proposed Solution

A macro sqlc.cast() that will do the type casting in the generated go struct, but not in the generated query?

-- -- name: GetRoomsGroupWithRooms :many
SELECT 
 sqlc.embed(rooms_groups),
 COALESCE(
 json_agg(rooms) FILTER (WHERE rooms.id IS NOT NULL),
 '[]'
 )::sqlc.cast(room[]) AS rooms

but the query won't include the ::sqlc.cast(room[]) part at all

What database engines need to be changed?

PostgreSQL

What programming language backends need to be changed?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /