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

Scanning one column into pgtype #33

Closed
AubSs opened this issue Apr 1, 2021 · 4 comments
Closed

Scanning one column into pgtype #33

AubSs opened this issue Apr 1, 2021 · 4 comments

Comments

@AubSs
Copy link

AubSs commented Apr 1, 2021

How to scan one column (and one or multiple rows) into pgx type ?

I'm getting errors like scany: scan row into struct fields: can't scan into dest[0]: cannot assign &{[some bytes ...] 2} into *[]pgtype.UUID

all my attempts ended in failures ..

-- sql : SELECT array_agg(uuid) FROM ...
var ids pgtype.UUIDArray
if err := pgxscan.Get(ctx, pg.Client(tx), &ids, sql, args...); err != nil {
    return nil, err
}

------ or

-- sql : SELECT uuid FROM ...
var ids []pgtype.UUID
if err := pgxscan.Select(ctx, pg.Client(tx), &ids, sql, args...); err != nil {
    return nil, err
}
@AubSs AubSs changed the title Scanning into pgtype Scanning one column into pgtype Apr 1, 2021
@georgysavva
Copy link
Owner

Hi. Thanks for opening this issue.
Currently, it's not possible to scan single-column rows into pgtype like that (as you would do it with string or int). The problem happens because pgtype.UUID is actually a struct and scany treats that case as scanning into a struct.

The workaround for your situation would be the following:

-- sql : SELECT uuid FROM ...
type Row struct {
    ID pgtype.UUID
}
var rows []Row
if err := pgxscan.Select(ctx, pg.Client(tx), &rows, sql, args...); err != nil {
    return nil, err
}
var ids []pgtype.UUID
// extract ids from rows

I will think about some kind of exceptions for scany to support that case without workarounds.

@AubSs
Copy link
Author

AubSs commented May 3, 2021

Ok, thx !

@georgysavva georgysavva mentioned this issue Sep 1, 2021
georgysavva added a commit that referenced this issue Dec 23, 2021
This PR adds an exception for scannable types to be treated as primitive types rather than structs or maps.
Fixes: #63, #33, #73.
@georgysavva
Copy link
Owner

@AubSs Hi! I just released a new version of scany that now includes this feature.

@AubSs
Copy link
Author

AubSs commented Feb 3, 2022

@georgysavva Hello! I just use the new functionality, and it's perfectly working!
Many thanks to you!

@AubSs AubSs closed this as completed Feb 3, 2022
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