broken netip.Addr scan support v0.25.1-0.20240516073824-99941dbd5078
gstarikov opened this issue · comments
Hi. trying to migrate from v0.22.1 to v0.25.1-0.20240516073824-99941dbd5078 and:
- fetching from db into netip_Addr fails -
unsupported Scan, storing driver.Value type string into type *netip.Addr
how to reproduce:
- take postgres 16.2
- create table
create table n (addr inet );
- and just go run github.com/stephenafamo/bob/gen/bobgen-psql@v0.25.1-0.20240516073824-99941dbd5078
and there will be error - unsupported Scan
Scan error on column index 0, name "addr": unsupported Scan, storing driver.Value type string into type *netip.Addr
rows, err := models.N.InsertMany(ctx, t.DB, n...)
I see where this bug comes from. I somehow didn't take into account that netip.Addr
does not implement the Scan
interface.
I'll have to create some wrapper type for it 🤦🏾
Should be fixed in v0.26.0
. Let me know if it persists
nope. it is still there.
unsupported Scan, storing driver.Value type string into type *netip.Addr
// Network is an object representing the database table.
type Network struct {
ID int64 `db:"id,pk,generated" `
NetworkAddr null.Val[netip.Addr] `db:"network_addr,generated" `
}
...
models.Networks.InsertMany(ctx, t.DB, networks...)
and here is created target to *null.Val[netip.Addr] https://github.com/stephenafamo/scan/blob/main/row.go#L68
You have to regenerate the models. It should now use a wrapper type which can be scanned into
oh, it's my fault. I forgot to update the generator to 0.26.
but with the new one (0.26) in the same place there is another problem.
Scan error on column index 5, name "network_addr": ParseAddr("10.11.0.0/24"): unexpected character (at "/24")
the postgres inet type can also handle subnets
https://www.postgresql.org/docs/current/datatype-net-types.html#DATATYPE-INET
The inet type holds an IPv4 or IPv6 host address, and optionally its subnet, all in one field.
The input format for this type is address/y where address is an IPv4 or IPv6 address and y is the number of bits in the netmask. If the /y portion is omitted, the netmask is taken to be 32 for IPv4 or 128 for IPv6, so the value represents just a single host. On display, the /y portion is suppressed if the netmask specifies a single host.
so its better to use netip.Prefix instead of netip.Addr
so its better to use netip.Prefix instead of netip.Addr
Great suggestion 👍🏾
This should now be fixed by #217.
Try it out and let me know.
Thanks a lot. everything is working. please tell me when to expect the tagged version?
Tagged v0.26.1
now.