-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Can't scan a null into a *string #34
Comments
The old code returned an empty The error is returned by the
You must use http://golang.org/pkg/database/sql/#NullString if the column may contain NULL values |
Thanks, that makes sense. I almost want a way to configure the library to give me the old behavior. In some ways, Go's "zero value as default" makes a lot of sense and eliminates a ton of boilerplate code. I'd kind of like to put that into one place, configurably, instead of scattering it everywhere in my code that I might encounter a NULL. Using the NullString, etc is pretty ugly code-wise. NULL is just a nightmare and I want to get rid of NULLs. I explicitly define every column in my tables as NOT NULL, but occasionally a command like SHOW PROCESSLIST is going to have a NULL in it, and my code just doesn't care; I want an empty string instead. I'm just going to use a NullString, and ignore its .Valid property and just get its .String, which will be empty if the column was NULL. Being able to tell the driver to convert NULL to zero-value for the type (or actually, I guess what it'd really do is skip scanning the column into the variable) would reduce a lot of hard and error prone (and not-future-proof) work for me. Or, if I don't want all the boilerplate code, I can take my chances that the column a) is really non-nullable b) will stay that way forever. |
I'd also prefer the database/sql package to follow Go's "default zero value" policy. You could still differentiate with the Null* types if you need to, but unfortunately the design decision was made this way. Maybe they change it in Go2 (+1 from me for that). |
I tried to add an option to zero |
I don't quite understand how the driver actually scans, but instead of |
I updated the branch. You can try it out for yourself if you want. |
That looks perfect for my needs, and I'll try it out next time I update our |
Another workaround came just to my mind:
|
Maybe keep this open, maybe good for the Examples Solution: https://github.com/guregu/null |
Since this page still shows up fairly high in search results, my two cents: You can also solve it in the part where, imo, the problem actually lies: The database abstraction level. You can work around this problem by doing the following: SELECT
id,
COALESCE(name, '') as name
FROM users This way, if |
I don't understand why did throws a exception when the string is null |
This example works for me |
You can pass a string pointer to deal with it like this:
it works fine for me. |
Besides @Dynom great comment, I think can still be worth mentioning that the Additionally, it preserves the ability to distinguish between a |
With nullstring, the dev is required to create a map vo if want to show the information like a json for example. No problem here, is a good and correct approach but it's not make sense for all cases then can be a additional work by a language limitation. |
the issue with sql.NullString is all the additional boilerplate to unMarshal when interfacing with many different micro-services |
I used to use the code from Google Code. After updating to the latest code in master, I'm getting the following error I didn't get previously:
sql: Scan error on column index 7: unsupported driver -> Scan pair: <nil> -> *string
I will investigate more, but perhaps in the meantime you know something about this already?
The text was updated successfully, but these errors were encountered: