devroom.io/content/posts/2009-08-20-once-and-for-all-rails-migrations-integer-limit-option.md
2019-06-05 14:32:16 +02:00

58 lines
1.1 KiB
Markdown

+++
date = "2009-08-20"
title = "Once and for all: Rails migrations integer :limit option"
tags = ["MySQL", "Ruby on Rails", "Rails", "sql", "migrations", "integer", "limit"]
slug = "once-and-for-all-rails-migrations-integer-limit-option"
+++
I 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):
<!--more-->
<table>
<tr>
<th>:limit</th>
<th>Numeric Type</th>
<th>Column Size</th>
<th>Max value</th>
<tr>
<tr>
<td>1</td>
<td>tinyint</td>
<td>1 byte</td>
<td>127</td>
</tr>
<tr>
<td>2</td>
<td>smallint</td>
<td>2 bytes</td>
<td>32767</td>
</tr>
<tr>
<td>3</td>
<td>mediumint</td>
<td>3 byte</td>
<td>8388607</td>
</tr>
<tr>
<td>nil, 4, 11</td>
<td>int(11)</td>
<td>4 byte</td>
<td>2147483647</td>
</tr>
<tr>
<td>5..8</td>
<td>bigint</td>
<td>8 byte</td>
<td>9223372036854775807</td>
</tr>
</table>
<em>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.</em>