Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change field type in Ecto?

I have a schema:

schema "editables" do     field :title, :string     field :content, :string      timestamps   end 

Now I want to change the type of one field form :integer to :binary. What's the correct way of writing the migration because using add is not working...?

def change do     alter table(:editables) do       add :title, :binary       add :content, :binary        timestamps     end   end 
like image 867
Paulo Janeiro Avatar asked Sep 15 '15 10:09

Paulo Janeiro


People also ask

What is Ecto type?

Ecto provides two types of custom types: basic types and parameterized types. Basic types are simple, requiring only four callbacks to be implemented, and are enough for most occasions. Parameterized types can be customized on the field definition and provide a wide variety of callbacks.

What does ecto migrate do?

Migration behaviour (Ecto SQL v3. 8.3) Migrations are used to modify your database schema over time. This module provides many helpers for migrating the database, allowing developers to use Elixir to alter their storage in a way that is database independent.

What is a changeset Ecto?

8.4) Changesets allow filtering, casting, validation and definition of constraints when manipulating structs. There is an example of working with changesets in the introductory documentation in the Ecto module. The functions cast/4 and change/2 are the usual entry points for creating changesets.


1 Answers

You have to use modify/3 to change the type. add/3 is only for adding new columns.

alter table(:editables) do   modify :content, :binary end 
like image 75
Gazler Avatar answered Nov 02 '22 13:11

Gazler