You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For postgres table that contains integer array type e.g. "related-number" in example given below
CREATE TABLE IF NOT EXISTS test(
id BIGSERIAL PRIMARY KEY,
number integer,
title text,
label_name text[] not null default '{}',
related_number int[] not null default '{}',
);
Corresponding golang struct will generate
pq.GenericArray type for related_number this will fail while scanning if related_number field is empy(not to be confused with null)
Right mapping should be "pq.Int32Array"
This is happening because pqArrMapping in postgres.go is incorrect
This can be fixed either in xo by correcting pqArrMapping or by overriding go.go teamplate
like this
// PQPostgresGoType parses a type into a Go type based on the databate type definition.
//
// For array types, it returns the equivalent as defined in github.com/lib/pq.
func PQPostgresGoType(d xo.Type, schema, itype, _ string) (string, string, error) {
goType, zero, err := loader.PostgresGoType(d, schema, itype)
if err != nil {
return "", "", err
}
if d.IsArray {
arrType, ok := pqArrMapping[goType]
goType, zero = "pq.GenericArray", "pg.GenericArray{}" // is of type struct { A any }; can't be nil
if ok {
goType, zero = arrType, "nil"
}
}
return goType, zero, nil
}
The text was updated successfully, but these errors were encountered:
For postgres table that contains integer array type e.g. "related-number" in example given below
CREATE TABLE IF NOT EXISTS test(
id BIGSERIAL PRIMARY KEY,
number integer,
title text,
label_name text[] not null default '{}',
related_number int[] not null default '{}',
);
Corresponding golang struct will generate
pq.GenericArray type for related_number this will fail while scanning if related_number field is empy(not to be confused with null)
Right mapping should be "pq.Int32Array"
This is happening because pqArrMapping in postgres.go is incorrect
This can be fixed either in xo by correcting pqArrMapping or by overriding go.go teamplate
like this
// custom override loader.PostgresGoType
var pqArrMapping = map[string]string{
"bool": "pq.BoolArray",
"[]byte": "pq.ByteArray",
"float64": "pq.Float64Array",
"float32": "pq.Float32Array",
"int64": "pq.Int64Array",
"int32": "pq.Int32Array",
"string": "pq.StringArray",
"int": "pq.Int32Array",
// default: "pq.GenericArray"
}
// PQPostgresGoType parses a type into a Go type based on the databate type definition.
//
// For array types, it returns the equivalent as defined in
github.com/lib/pq
.func PQPostgresGoType(d xo.Type, schema, itype, _ string) (string, string, error) {
goType, zero, err := loader.PostgresGoType(d, schema, itype)
if err != nil {
return "", "", err
}
if d.IsArray {
arrType, ok := pqArrMapping[goType]
goType, zero = "pq.GenericArray", "pg.GenericArray{}" // is of type struct { A any }; can't be nil
if ok {
goType, zero = arrType, "nil"
}
}
return goType, zero, nil
}
The text was updated successfully, but these errors were encountered: