Skip to content

TEXT[] does not support NULL #171

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

Closed
maxhawkins opened this issue Dec 14, 2019 · 2 comments
Closed

TEXT[] does not support NULL #171

maxhawkins opened this issue Dec 14, 2019 · 2 comments
Labels
bug Something isn't working

Comments

@maxhawkins
Copy link
Contributor

maxhawkins commented Dec 14, 2019

CREATE TABLE foo (bar TEXT[])

Generates

type Foo struct {
  Bar []string
}

Which fails to scan if bar = NULL. Instead, maybe it should be:

type Foo struct {
  Bar pq.StringArray
}

which handles NULL.

@kyleconroy kyleconroy added the bug Something isn't working label Dec 16, 2019
@kyleconroy
Copy link
Collaborator

Which fails to scan if bar = NULL.

This actually isn't true. If bar is NULL, Scan will return an empty slice.

CREATE TABLE foo (
        id text,
        tags text[]
);

INSERT INFO foo (id, tags) VALUES ('foo-array', NULL);

-- name: ListFoo :many
SELECT * FROM foo; 

Calling ListFoo will succeed and return a single row with an empty slice and null ID.

rows, err := q.ListFoo(ctx)                                                                                                                                                           log.Println(rows)
$ 2019/12/16 13:51:41 [{{foo-array true} []}]

However, if the array contains a null item, the Scan will fail.

playground=# insert into foo (id, tags) values ('foo-array', array[NULL]);
INSERT 0 1
playground=# \q
$ ./sqlc-playground 
2019/12/16 13:54:07 []
2019/12/16 13:54:07 sql: Scan error on column index 1, name "tags": pq: parsing array element index 0: cannot convert nil to string

I've opened #185 to track the issue with NULL array elements.

@maxhawkins
Copy link
Contributor Author

You're right, I was mistaken here. My issue was related to some old sqlx code that the sqlc code was interacting with that had the wrong types.

Switching it to sqlc revealed the type issue and fixed my problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants