Fix insert and randomize errors, fix time.Time
* MySQL requires timeParsing enabled * Update readme to reflect this requirement
This commit is contained in:
parent
a164540276
commit
fdeecbf106
5 changed files with 44 additions and 7 deletions
11
README.md
11
README.md
|
@ -179,6 +179,10 @@ fmt.Println(len(users.R.FavoriteMovies))
|
|||
a *composite primary key* that encompasses both foreign table foreign keys. For example, on a
|
||||
join table named `user_videos` you should have: `primary key(user_id, video_id)`, with both `user_id`
|
||||
and `video_id` being foreign key columns to the users and videos tables respectively.
|
||||
* For MySQL if using the `github.com/go-sql-driver/mysql` driver, please activate
|
||||
[time.Time parsing](https://github.com/go-sql-driver/mysql#timetime-support) when making your
|
||||
MySQL database connection. SQLBoiler uses `time.Time` and `null.Time` to represent time in
|
||||
it's models and without this enabled any models with `DATE`/`DATETIME` columns will not work.
|
||||
|
||||
### Pro Tips
|
||||
* Foreign key column names should end with `_id`.
|
||||
|
@ -1060,6 +1064,13 @@ with all Postgres drivers. Example:
|
|||
|
||||
Please note that multi-dimensional Postgres ARRAY types are not supported at this time.
|
||||
|
||||
#### Why aren't my time.Time or null.Time fields working in MySQL?
|
||||
|
||||
For MySQL if using the `github.com/go-sql-driver/mysql` driver, please activate
|
||||
[time.Time parsing](https://github.com/go-sql-driver/mysql#timetime-support) when making your
|
||||
MySQL database connection. SQLBoiler uses `time.Time` and `null.Time` to represent time in
|
||||
it's models and without this enabled any models with `DATE`/`DATETIME` columns will not work.
|
||||
|
||||
#### Where is the homepage?
|
||||
|
||||
The homepage for the [SQLBoiler](https://github.com/vattle/sqlboiler) [Golang ORM](https://github.com/vattle/sqlboiler) generator is located at: https://github.com/vattle/sqlboiler
|
||||
|
|
|
@ -47,6 +47,10 @@ func MySQLBuildQueryString(user, pass, dbname, host string, port int, sslmode st
|
|||
config.Addr += ":" + strconv.Itoa(port)
|
||||
config.TLSConfig = sslmode
|
||||
|
||||
// MySQL is a bad, and by default reads date/datetime into a []byte
|
||||
// instead of a time.Time. Tell it to stop being a bad.
|
||||
config.ParseTime = true
|
||||
|
||||
return config.FormatDSN()
|
||||
}
|
||||
|
||||
|
@ -256,7 +260,9 @@ func (m *MySQLDriver) TranslateColumnType(c bdb.Column) bdb.Column {
|
|||
c.Type = "null.Int8"
|
||||
case "smallint":
|
||||
c.Type = "null.Int16"
|
||||
case "mediumint", "int", "integer":
|
||||
case "mediumint":
|
||||
c.Type = "null.Int32"
|
||||
case "int", "integer":
|
||||
c.Type = "null.Int"
|
||||
case "bigint":
|
||||
c.Type = "null.Int64"
|
||||
|
@ -281,10 +287,12 @@ func (m *MySQLDriver) TranslateColumnType(c bdb.Column) bdb.Column {
|
|||
c.Type = "int8"
|
||||
case "smallint":
|
||||
c.Type = "int16"
|
||||
case "mediumint", "int", "integer":
|
||||
case "mediumint":
|
||||
c.Type = "int32"
|
||||
case "int", "integer":
|
||||
c.Type = "int"
|
||||
case "bigint":
|
||||
c.Type = "null.Int64"
|
||||
c.Type = "int64"
|
||||
case "float":
|
||||
c.Type = "float32"
|
||||
case "double", "double precision", "real":
|
||||
|
|
|
@ -49,7 +49,7 @@ var (
|
|||
rgxValidTime = regexp.MustCompile(`[2-9]+`)
|
||||
|
||||
validatedTypes = []string{
|
||||
"inet", "line", "uuid", "interval",
|
||||
"inet", "line", "uuid", "interval", "mediumint",
|
||||
"json", "jsonb", "box", "cidr", "circle",
|
||||
"lseg", "macaddr", "path", "pg_lsn", "point",
|
||||
"polygon", "txid_snapshot", "money", "hstore",
|
||||
|
@ -221,6 +221,13 @@ func randomizeField(s *Seed, field reflect.Value, fieldType string, canBeNull bo
|
|||
field.Set(reflect.ValueOf(value))
|
||||
return nil
|
||||
}
|
||||
case typeNullInt32:
|
||||
if fieldType == "mediumint" {
|
||||
// 8388607 is the max for 3 byte int
|
||||
value = null.NewInt32(int32(s.nextInt())%8388607, true)
|
||||
field.Set(reflect.ValueOf(value))
|
||||
return nil
|
||||
}
|
||||
case typeNullJSON:
|
||||
value = null.NewJSON([]byte(fmt.Sprintf(`"%s"`, randStr(s, 1))), true)
|
||||
field.Set(reflect.ValueOf(value))
|
||||
|
@ -287,6 +294,13 @@ func randomizeField(s *Seed, field reflect.Value, fieldType string, canBeNull bo
|
|||
field.Set(reflect.ValueOf(value))
|
||||
return nil
|
||||
}
|
||||
case reflect.Int32:
|
||||
if fieldType == "mediumint" {
|
||||
// 8388607 is the max for 3 byte int
|
||||
value = int32(s.nextInt()) % 8388607
|
||||
field.Set(reflect.ValueOf(value))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
switch typ {
|
||||
case typeJSON:
|
||||
|
|
|
@ -69,7 +69,7 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
|
|||
|
||||
if len(cache.retMapping) != 0 {
|
||||
{{if .UseLastInsertID -}}
|
||||
cache.retQuery = fmt.Sprintf("SELECT %s FROM {{$schemaTable}} WHERE %s", strings.Join(returnColumns, "{{.LQ}},{{.RQ}}"), strmangle.WhereClause("{{.LQ}}", "{{.RQ}}", {{if .Dialect.IndexPlaceholders}}1{{else}}0{{end}}, {{$varNameSingular}}PrimaryKeyColumns))
|
||||
cache.retQuery = fmt.Sprintf("SELECT {{.LQ}}%s{{.RQ}} FROM {{$schemaTable}} WHERE %s", strings.Join(returnColumns, "{{.LQ}},{{.RQ}}"), strmangle.WhereClause("{{.LQ}}", "{{.RQ}}", {{if .Dialect.IndexPlaceholders}}1{{else}}0{{end}}, {{$varNameSingular}}PrimaryKeyColumns))
|
||||
{{else -}}
|
||||
cache.query += fmt.Sprintf(" RETURNING {{.LQ}}%s{{.RQ}}", strings.Join(returnColumns, "{{.LQ}},{{.RQ}}"))
|
||||
{{end -}}
|
||||
|
|
8
testdata/mysql_test_schema.sql
vendored
8
testdata/mysql_test_schema.sql
vendored
|
@ -62,8 +62,12 @@ CREATE TABLE magic (
|
|||
time_nine timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
time_eleven date NULL,
|
||||
time_twelve date NOT NULL,
|
||||
time_fifteendate NULL DEFAULT '19990108',
|
||||
time_sixteen date NOT NULL DEFAULT '1999-01-08',
|
||||
time_fifteen date NULL DEFAULT '19990108',
|
||||
time_sixteen date NOT NULL DEFAULT '1999-01-08'
|
||||
);
|
||||
|
||||
CREATE TABLE magicest (
|
||||
id int primary key not null auto_increment,
|
||||
aa json NULL,
|
||||
bb json NOT NULL,
|
||||
kk double precision NULL,
|
||||
|
|
Loading…
Add table
Reference in a new issue