-
Notifications
You must be signed in to change notification settings - Fork 374
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
What to do with NULL values? #77
Comments
Hi there, The type Person struct {
Id int64
Name sql.NullString
Age sql.NullInt64
} Name and Age can both be null in the db, and will scan properly. You would get the value via: person.Name.String. |
This commit adds support for storing and retriving time.Time objects, by passing through such objects to the underlying driver. This resolves gorp issue go-gorp#14. The commit is tested and works with the github.com/mattn/go-sqlite3 driver. The commit does not currently work with the github.com/ziutek/mymysql/godrv driver, due to a bug in the mysql driver. This problem is resolved by mymysql pull request go-gorp#77. I have successfully tested that with the fix from pull request go-gorp#77 applied to mymysql, and with the current commit applied, gorp can correctly store and retrieve time.Time objects from mysql databases. This commit is NOT tested with the github.com/lib/pq driver.
Thanks for this - I hadn't seen "sql.NullInt64" etc before. |
sql.NullString solves my problem. But it is not convenient. Wonder why string can not be used directly for mapping. Is it not possible to assign an empty string when there is a null? |
Same question as @vinceyuan |
An empty string is not a null string - they are considered two different values in the database and in go, and plenty of code relies on checking for If you just feel like If you just don't like doing more than one comparison (which the pointer doesn't really solve), but empty string and null are equivalent in your database, you can implement package foo
import (
"database/sql"
"database/sql/driver"
)
type NullToEmptyString string
func (n NullToEmptyString) Value() (driver.Value, error) {
return string(n), nil
}
func (n *NullToEmptyString) Scan(v interface{}) error {
if v == nil {
*n = ""
return nil
}
s, ok := v.(string)
if !ok {
return fmt.Errorf("Cannot scan value %v: expected string type, got %T", v, v)
}
*n = NullToEmptyString(s)
return nil
} |
Question (request for doc enhancement :)
I have a database which contains NULL values (mysql). When I try to fill a struct using
I get this error:
Which makes sense, since I have NULL values in the table which cannot be converted to an int (the requested column / struct type is int).
Is there any automatic way to treat NULL values as 0 (for example) without changing the database?
The text was updated successfully, but these errors were encountered: