Use smaller string column than varchar255

I want to create a column in one of the tables with a varchar2 and not more than that. By default string in ecto migration refers varchar255. So how can I use varchar2?

In migration simply write: :"varchar(2)" as a type or "varchar(2)" in latest ecto_sql (as String was not supported before).

3 Likes

Thank you :slight_smile:

You can also use the size option in the migration. Here’s an example from the ecto_sql tests:

Note how name is generated as a VARCHAR(20).

5 Likes

Ah, completely forgot about this option, nice find. In that case it’s worth to give some links to official API:

Each of those 3 mentions :size option. Both add/3 and modify/3 provides more useful options:
https://hexdocs.pm/ecto_sql/Ecto.Migration.html#module-field-types
https://hexdocs.pm/ecto_sql/Ecto.Migration.html#add/3
https://hexdocs.pm/ecto_sql/Ecto.Migration.html#modify/3

4 Likes

Thank you. This is a more clean option :slight_smile: