sqlserver icon indicating copy to clipboard operation
sqlserver copied to clipboard

uint8 in Golang should correspond to tinyint in SQL Server

Open iTanken opened this issue 1 year ago • 0 comments

https://github.com/go-gorm/sqlserver/blob/ef8f762cc01457cd331d3d93cf5648f2d8dc5661/sqlserver.go#L188-L202

  • https://learn.microsoft.com/zh-cn/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql?view=sql-server-ver16

Because in SQL Server, the range of values for the tinyint type is from 0 to 255, which exactly matches the range of uint8, so when field.Size < 16 and field.DataType == schema.Uint, should sqlType be tinyint? Like this:

 case schema.Int, schema.Uint:
 	var sqlType string
 	switch {
 	case field.Size < 16:
		if field.DataType == schema.Uint {
			sqlType = "tinyint"
		} else {
			sqlType = "smallint"
		}
 	case field.Size < 31:
 		sqlType = "int"
 	default:
 		sqlType = "bigint"
 	}
  
 	if field.AutoIncrement {
 		return sqlType + " IDENTITY(1,1)"
 	}
 	return sqlType

iTanken avatar Mar 07 '24 02:03 iTanken