DEV Community

Renata
Renata

Posted on

How to change column to nullable with modify in Ecto migration?

Sooner or later you'll have to change the null constraint in one of your DB relations. How to do it easily in Ecto?

Although I came across many different examples where a raw SQL has been used to perform this type of operation in Ecto, it's actually super easy to do it with modify/3 function.

Let's assume that your migration looks like this:

create table(:blog_posts) do
  add :title, :string, null: false
  add :intro, :text, null: false
  add :body, :text, null: false
  add :category_id, references(:blog_categories, on_delete: :delete_all)
end
Enter fullscreen mode Exit fullscreen mode

Read more...

Top comments (2)

Collapse
 
sloan profile image
Sloan the DEV Moderator

Hi there, we encourage authors to share their entire posts here on DEV, rather than mostly pointing to an external link. Doing so helps ensure that readers don’t have to jump around to too many different pages, and it helps focus the conversation right here in the comments section.

If you choose to do so, you also have the option to add a canonical URL directly to your post.

Collapse
 
adamcstephens profile image
Adam Stephens

This post doesn't meet the site rules.

Posts must contain substantial content — they may not merely reference an external link that contains the full post.