Using a function with custom types? #2603
-
using the following sql: -- name: CreateReport :one
INSERT INTO reports
(title, description, location)
VALUES
($1, $2, ST_GEOMFROMTEXT($3))
RETURNING id, title, description, location; I get the following generated struct: type CreateReportParams struct {
Title string `json:"title"`
Description string `json:"description"`
StGeomfromtext interface{} `json:"st_geomfromtext"`
} As you can see for some reason the ST_GEOMFROMTEXT function is interpreted as a variable. The correct output should be: type CreateReportParams struct {
Title string `json:"title"`
Description string `json:"description"`
Location types.PostGISPoint `json:"location"`
} Why does this happen? How can I deal with this? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
According to https://postgis.net/docs/ST_GeomFromText.html, shouldn't $3 be TEXT? |
Beta Was this translation helpful? Give feedback.
-
Not exactly. It's a bit confusing, but
I will assume by "this" you mean "the struct field for the @orisano provides a good explanation above of the type inference issues involved. The But it may satisfy your needs to simply use an explicit cast to get |
Beta Was this translation helpful? Give feedback.
-
@tamis-laan have you managed to fix this? If I use fmt.Sprintf("SRID=4326;POINT(%.8f %.8f)", X , Y) I still get interface {} is string, not []uint8 |
Beta Was this translation helpful? Give feedback.
Not exactly. It's a bit confusing, but
sqlc
has simply picked a name for the parameter struct field based on the function name. If you aren't happy with the name, you can usesqlc.arg()
to change it like this:($1, $2, ST_GEOMFROMTEXT(sqlc.arg(location)))
.I will assume by "this" you mean "the struct field for the
location
query parameter has typeinterface{}
rather thantypes.PostGISPoint
" since I've already addressed the naming issue above.@orisano provides a good explanation above of the type inference issues involved. The
ST_GEOMFROMTEXT
functi…