0

I have a problem:

I have this struct in GO:

type Person struc{
   name string
   age int64
}

and I want to save it in a MYSQL table person with name varchar(255) and age int(11)

Im using the ExecContext function but it returns me Error 1265: Data truncated for column 'age' at row 1

Please if some one can help me with this it would be really nice, this are examples, not the real data, but is the problem actually

  • 3
    Edit the question to show the call to ExecContext and the actual values of the two fields. – Charlie Tumahai Jun 11 '21 at 17:27
  • Are you using gorm? Lowercase fields will often not be accessed by APIs which use reflection. – Zyl Jun 11 '21 at 21:58
  • 1
    Go int64 have a wider range than mysql INT. You are trying to store a value that is larger then the maximum possible in MYSQL (or less than the least). It is doubtful that such a value would be a person's valid age, so you probably have some additional bugs going on somewhere. Have you tried printing out the values you are storing? – BadZen Jun 12 '21 at 18:03
  • 1
    LOL - in what units of time are you measuring the person's age, picoseconds? :-) – Bill Karwin Jun 16 '21 at 20:09
  • 1
    `INT` in MySQL is 32 bit. For 64 bit you need the `BIGINT` datatype. Needing more than 32 bits (or even 8 bits, which gives -128 to 127) for an age makes your data highly dubious. – MatBailie Jul 23 '21 at 22:04

1 Answers1

0

As @BadZen said, this error is warning you that the value stored in age as an int64 is greater than the maximum value allowed for int(11) (which is 2,147,483,647, regardless of the 11 in int as @MatBailie pointed out). This is likely happening because int64 is passing a 64 bit integer (as per its definition) into MySQL when as said in comments, a MySQL int only holds 32 bits.

Rather than increasing your age type to a BIGINT, which you shouldn't need for an age, just use a 32 bit int in Go (int32).

Using the int data type that Go provides, I believe this will be 32 bits on a 32 bit system and 64 bits on a 64 bit system. I haven't had trouble with using it before, but to play it safe, you could use int32 instead of int.

Aaron
  • 132
  • 10
  • 1
    In `INT(11)` the `11` specifies the print formatting of the data type *(padded to 11 digits)*, not the size of the dta type. `INT(3)`, `INT(11)`, etc, are all 32 bit `INT`. To hold a wider range MySQL uses `BIGINT`. https://stackoverflow.com/questions/5634104/what-is-the-size-of-column-of-int11-in-mysql-in-bytes – MatBailie Jul 23 '21 at 22:07
  • @MatBailie I didn't know this, thanks so much for the heads up. I will look at the docs but do you mind explaining what the purpose of that print formatting is? For example, if I put in a 12 digit number, would it not accept it for print formatting reasons? Or would it only display the first 11 digits? I will check that out on my own, but if you have a good doc link explaining that, I'd greatly appreciate it – Aaron Jul 24 '21 at 23:37
  • 1
    It will pad up to 11 characters, it won't ever truncate anything. It's all explained in the link I provided, which also links to the docs. Also, as `INT` is 32 bit, the range is `-2,147,483,648 to 2,147,483,647`, this means that providing a 12 digit number would cause an overflow. You'd need `BIGINT` (64 bit) for that. – MatBailie Jul 25 '21 at 11:45