Closed GetPsyched closed 1 year ago
The problem occurs in the columns with the ::JSONB
type cast.
Struct that SQLC generated with lib/pq
: (this is correct and works)
type GetClubRow struct {
Name string `json:"name"`
Alias string `json:"alias"`
Category string `json:"category"`
ShortDescription string `json:"short_description"`
Email string `json:"email"`
IsOfficial bool `json:"is_official"`
Description json.RawMessage `json:"description"`
Admins json.RawMessage `json:"admins"`
Branch []string `json:"branch"`
Faculties json.RawMessage `json:"faculties"`
Socials json.RawMessage `json:"socials"`
}
Struct that SQLC generated with pgx
: (this breaks my API)
type GetClubRow struct {
Name string `json:"name"`
Alias string `json:"alias"`
Category string `json:"category"`
ShortDescription string `json:"short_description"`
Email string `json:"email"`
IsOfficial bool `json:"is_official"`
Description []byte `json:"description"`
Admins []byte `json:"admins"`
Branch []string `json:"branch"`
Faculties []byte `json:"faculties"`
Socials []byte `json:"socials"`
}
Some differences between drivers are expected. I'm not sure if this particular difference is "expected" or not, but it would be a significant breaking change to alter the types for pgx users in this case.
I think you can use an override for the jsonb
type though, which would be an easy solution. Here's a playground URL with a working example pulled from a portion of your query: https://play.sqlc.dev/p/4dbf9b25f695e6ab72f56e3764f96d29a82450176fccf0f32ca47f5ea8b94a47
Here's the config extracted for convenience:
version: 2
sql:
- engine: postgresql
queries: query.sql
schema: query.sql
gen:
go:
package: "db"
out: "."
sql_package: "pgx/v5"
overrides:
- db_type: jsonb
go_type:
import: "encoding/json"
type: "RawMessage"
- db_type: jsonb
go_type:
import: "encoding/json"
type: "RawMessage"
nullable: true
Hey, @andrewmbenton, thanks a lot for the config! It almost works. Unfortunately, it's trying to import json.RawMessage
from some json
lib rather than encoding/json
from where it actually is. Do you know of a work-around for this? Sorry, I'm not versed enough in SQLC configuration.
Unfortunately, it's trying to import
json.RawMessage
from somejson
lib rather thanencoding/json
from where it actually is.
Oh yeah sorry, that was me being a little too hasty. I'll edit my original reply with a corrected config and playground link.
Thanks a ton for the help! You've ended a lot of frustration on my end haha. Switching to pgx
had broken my APIs that I had to rollback. I think I can finally move forward now :)
Version
1.21.0
What happened?
Using
lib/pq
as my preferred db driver for my PostgreSQL db, SQLC generatedjson.RawMessage
types within a struct for column X in a SELECT query. After switching topgx
, it generates[]byte
for the same columns. This breaks my APIs as it sends malformed data which needs to be post processed if I want to fix it that way.Relevant log output
No response
Database schema
No response
SQL queries
Configuration
Playground URL
No response
What operating system are you using?
Linux
What database engines are you using?
PostgreSQL
What type of code are you generating?
Go