Migration

DB schema_migration table used to know which migrations have yet to run

Usage

  1. mix ecto.gen.migration [action_name]

  2. Edit in priv/repo/migrations

defmodule ReactPhoenix.Repo.Migrations.ChangeGid do
  use Ecto.Migration

  def change do
    alter table(:users) do
      add :address, :string
      add :usertype_id, references(:usertypes, on_delete: :nothing)
      add :event_id, references(:usertypes, on_delete: :delete_all, type: :string)
      add :my_array, {:array, inner_type}
      modify :title, :text
  		remove :views
    end
    
    #rename is outside alter statement
    rename table(:subevents), :parent, to: :event_id
    
    #order matters, if another column depends should remove before dropping
    drop table(:registration_question_options)
  end
end
  1. mix ecto.migrate

Can undo with mix ecto.rollback

On delete

  • :nothing - if any referencing rows still exist when the constraint is checked, an error is raised; this is the default behavior if you do not specify anything.

  • :delete_all - specifies that when a referenced row is deleted, row(s) referencing it should be automatically deleted as well

  • :nilify_all - causes the referencing column(s) in the referencing row(s) to be set to nil when the referenced row is deleted

  • :restrict - prevents deletion of a referenced row. It will fail if there is a referenced object.

UUID or Nanoid

Creating (will be uuid in table)

Referencing

Then you have to generate it when you create it, Ecto.UUID.generate()

Advanced Autogenerate Nanoid

More Example File

Change defines an up and down allowing rollback of db changes

Last updated