Skip to content
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

Fix DataType Define #111

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions postgres.go
Expand Up @@ -165,11 +165,11 @@ func (dialector Dialector) DataTypeOf(field *schema.Field) string {
} else {
switch {
case size <= 16:
return "smallint"
return "int2"
case size <= 32:
return "integer"
return "int4"
default:
return "bigint"
return "int8"
}
}
case schema.Float:
Expand All @@ -186,7 +186,8 @@ func (dialector Dialector) DataTypeOf(field *schema.Field) string {
}
return "text"
case schema.Time:
if field.Precision > 0 {
// in postgresql, Precision == 0, This means being accurate to the seconds.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default value of precision is 0, that why we ignored the zero value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default value of precision is 0, that why we ignored the zero value.

if set default value, it will set type = "timestamptz", it same as "timestamptz(6)".

in current logic, no way to define type "timestamptz(0)"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this changed the default behavior and removed the default precision?

Maybe we need to check field's tag by using field.TagSettings["PRECISION"] if the precision's value is 0?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change here doesn't seem to make sense, because there was a problem here, It only works if the user doesn't set type.
field.DataType is set by user, when we using gorm:"type:"timestamptz", it's not equal schema.Time, and when we using gorm:"type:"time", it's means time of day (no date).

https://www.postgresql.org/docs/14/datatype-datetime.html

Edit

MyTime time.Time `gorm:"precision:6"`

The reason why this triggers alter is because we mistakenly mistook precision for size
https://github.com/go-gorm/gorm/blob/master/migrator/migrator.go#L423

if field.Precision >= 0 && field.Precision <= 6 {
return fmt.Sprintf("timestamptz(%d)", field.Precision)
}
return "timestamptz"
Expand All @@ -210,11 +211,11 @@ func (dialectopr Dialector) RollbackTo(tx *gorm.DB, name string) error {
func getSerialDatabaseType(s string) (dbType string, ok bool) {
switch s {
case "smallserial":
return "smallint", true
return "int2", true
case "serial":
return "integer", true
return "int4", true
case "bigserial":
return "bigint", true
return "int8", true
default:
return "", false
}
Expand Down