# Varchar attribute with migration

**URL:** <https://www.ruby-forum.com/t/varchar-attribute-with-migration/68077>\
**Category:** Rails\
**Created:** [September 8, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/varchar-attribute-with-migration/68077 "2006-09-08T23:29:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [September 8, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/varchar-attribute-with-migration/68077/1 "2006-09-08T23:29:11Z")

</div>

I can’t seem to find this anywhere… How would I define the number in  
varchar(#) with the migration tool. By default when using :string it  
sets it at 255. Probably a really simple answer to this, thanks for  
the help.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [September 9, 2006, 12:55am UTC](https://www.ruby-forum.com/t/varchar-attribute-with-migration/68077/2 "2006-09-09T00:55:18Z")

</div>

:limit =\> n

On 08/09/06, salo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I can’t seem to find this anywhere… How would I define the number in  
> varchar(#) with the migration tool. By default when using :string it  
> sets it at 255. Probably a really simple answer to this, thanks for  
> the help.
> 
> >

–  
Rasputin :: Jack of All Trades - Master of Nuns  
[http://number9.hellooperator.net/](http://number9.hellooperator.net/)

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [September 9, 2006, 1:13am UTC](https://www.ruby-forum.com/t/varchar-attribute-with-migration/68077/3 "2006-09-09T01:13:19Z")

</div>

Take a look at :limit.

add\_column :your\_table, :your\_column, :string, :limit =\> 20

This page from the Rails API lists all the options.

> **[ActiveRecord::ConnectionAdapters::TableDefinition](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#M000659)**
>
> Represents the schema of an SQL table in an abstract way. This class provides methods for manipulating the schema representation.

Aaron

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [September 9, 2006, 1:29am UTC](https://www.ruby-forum.com/t/varchar-attribute-with-migration/68077/4 "2006-09-09T01:29:21Z")

</div>

> I can’t seem to find this anywhere… How would I define the number in  
> varchar(#) with the migration tool. By default when using :string it  
> sets it at 255. Probably a really simple answer to this, thanks for  
> the help.

There’s a reason that it defaults to 255. Because for the vast majority  
of cases, it Just Doesn’t Matter. It’s not worth dedicating brain  
cycles to try to figure out what the optimal size of a field is.  
Especially not with varchar, which automatically contracts and expands  
any way. You’ll usually guess too low anyway and then later have to  
change it.

David Heinemeier H.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [September 9, 2006, 2:39am UTC](https://www.ruby-forum.com/t/varchar-attribute-with-migration/68077/5 "2006-09-09T02:39:28Z")

</div>

thanks for the help and advice…

-salo
