jmoiron / modl

golang database modelling library
MIT License
479 stars 48 forks source link

How to use columns which may be NULL? #45

Open fd0 opened 8 years ago

fd0 commented 8 years ago

Hi, thanks for writing this library!

I don't understand how I can create a column that may be NULL in the database. Take a struct like the following:

type Person struct {
   ID int64
   BossID sql.NullInt64
}

When a database is created by modl, the type of the column BossID (at least for sqlite3) is text. Why is that?

I have found the code mapping a column to the type here: https://github.com/jmoiron/modl/blob/master/dialect.go#L111-L112 It seems that it does test for the type name NullableInt64 instead of NullInt64. Is that intentional or a bug? I was unable to find any reference to the string NullableInt64, except in an early discussion on how to name the types within sql.Null*.

Creating a custom type works, but I don't think that this is the way it is supposed to be:

type NullableInt64 sql.NullInt64

Now a database column with the type integer is created.

Am I missing something?

fd0 commented 8 years ago

For gorp, which modl is a fork of, this was changed in https://github.com/go-gorp/gorp/commit/30f5a47e7a7fb0e0f9b616502e0f316c66cdee70 https://github.com/go-gorp/gorp/pull/121