Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] array type is not generated correctly for integer array in case of postgres #380

Open
newbeelearn opened this issue Dec 12, 2022 · 1 comment

Comments

@newbeelearn
Copy link

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
}

@evassilyev
Copy link
Contributor

@newbeelearn I have the same problem. Thank you for suggested solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants