Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rails migrations integer :limit option
literally always have to look up the meaning of :limit in migrations when it comes to integer values. Here's an overview. Now let's memorise it (oh, this works for MySQL, other databases may work differently):
:limit Numeric Type Column Size Max value
1 tinyint 1 byte 127
2 smallint 2 bytes 32767
3 mediumint 3 byte 8388607
nil, 4, 11 int(11) 4 byte 2147483647
5..8 bigint 8 byte 9223372036854775807
Note: by default MySQL uses signed integers and Rails has no way (that I know of) to change this behaviour. Subsequently, the max. values noted are for signed integers.
@teddyzetterlund

This comment has been minimized.

Copy link

teddyzetterlund commented Aug 1, 2013

Also, Postgres doesn't support tinyint but Rails migrations handles that automatically and sets it to smallint.

@marcamillion

This comment has been minimized.

Copy link

marcamillion commented Mar 24, 2016

Thanks for this gist. Really saved my bacon tonight!

@segunadeleye

This comment has been minimized.

Copy link

segunadeleye commented Mar 8, 2017

Thanks for this important information 💯 👍

@sindhu-shree-1

This comment has been minimized.

Copy link

sindhu-shree-1 commented Oct 15, 2018

What's the default column size when :limit option is not specified?

@sindhu-shree-1

This comment has been minimized.

Copy link

sindhu-shree-1 commented Oct 15, 2018

What's the default column size when :limit option is not specified?

nvm. It's int(11) i.e. 4 bytes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.