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

fixed int data type selection based on size #53

Merged
merged 1 commit into from
Feb 20, 2022
Merged

fixed int data type selection based on size #53

merged 1 commit into from
Feb 20, 2022

Conversation

rafiulgits
Copy link
Contributor

@rafiulgits rafiulgits commented Feb 19, 2022

Pull request title
integer data type selection by sizing should be fixed


Conventions

  • Do only one thing
  • Non breaking API changes
  • Tested

Pull request reason
Fix sql server integer type selection based on go lang int value memory size


User Case Description

switch {

SQL Server datatype selection based on int value data size is not appropriate. Here is the condition

switch {
case field.Size < 16:
	sqlType = "smallint"
case field.Size < 31:
	sqlType = "int"
default:
	sqlType = "bigint"
}

Golang integers

  • int8 : 1 byte or 8 bits
  • int16: 2 bytes or 16 bits
  • int32: 3 bytes or 32 bits
  • int64 4 bytes or 64 bits`

SQL Server integers equivalent

  • tinyint : int8
  • smallint : int16
  • int : int32
  • biging : int64

The condition should be

switch {
case field.Size < 16:
	sqlType = "tinyint"
case field.Size < 31:
	sqlType = "smallint"
case field.Size < 64:
	sqlType = "int"
default:
	sqlType = "bigint"
}

)

@jinzhu jinzhu merged commit 80f0e92 into go-gorm:master Feb 20, 2022
@jinzhu
Copy link
Member

jinzhu commented Feb 20, 2022

reverted, tinyint equals int8, but size 15 will choose tinyint which will overflow?

@rafiulgits
Copy link
Contributor Author

rafiulgits commented Feb 20, 2022

but one more issue still happening,

when I define a model like this

ProductID       int       `gorm:"type:int;not null"`

sql server database migration make it bigint automatically.

Whereas for type:tinyint, type:smallint, type:bigint are working perfectly.
Can you describe this?

@rafiulgits
Copy link
Contributor Author

rafiulgits commented Feb 20, 2022

reverted, tinyint equals int8, but size 15 will choose tinyint which will overflow?

but size 15 will never come,
golang will select 8bit, 16bit, 32bit or 64bit.

If I am wrong then please correct my view.

Thanks @jinzhu

@jinzhu
Copy link
Member

jinzhu commented Feb 20, 2022 via email

@iTanken
Copy link
Contributor

iTanken commented Sep 26, 2024

reverted, tinyint equals int8, but size 15 will choose tinyint which will overflow?

Could we prevent overflow by using different types based on whether field.DataType is schema.Int or schema.Uint, for example, in #129:

e01b9ff#diff-d4f96c05ed87dd1db12826d078654e8cb281186d30b116ba5ced4832c414a234R190

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants