# \#ecto-migration

**URL:** https://forum.elixirforum.com/tag/ecto-migration/1342.md

[Latest](https://forum.elixirforum.com/latest.md) · [Categories](https://forum.elixirforum.com/categories.md) · [Tags](https://forum.elixirforum.com/tags.md)

---

## [How to model and enforce: "A table has zero or one of multiple specification associations"](https://forum.elixirforum.com/t/how-to-model-and-enforce-a-table-has-zero-or-one-of-multiple-specification-associations/73276)

<div class="topic-metadata">

**Author:** [@carlosfritz](https://forum.elixirforum.com/u/carlosfritz)\
**Replies:** 3\
**Last updated:** [November 12, 2025, 10:16pm UTC](https://forum.elixirforum.com/t/how-to-model-and-enforce-a-table-has-zero-or-one-of-multiple-specification-associations/73276 "2025-11-12T22:16:13Z")

</div>

I want to model the following relationship: A location has zero or one of multiple (currently 7) specifications (each specification is a has\_one association) The goals: Strict DB consistency. Unable to set more than …

---

## [Migrations on Fly.io (triggered from MyApp.Release.migrate()) & what does ash.migrate do?](https://forum.elixirforum.com/t/migrations-on-fly-io-triggered-from-myapp-release-migrate-what-does-ash-migrate-do/72734)

<div class="topic-metadata">

**Author:** [@zac](https://forum.elixirforum.com/u/zac)\
**Replies:** 4\
**Last updated:** [October 1, 2025, 9:05pm UTC](https://forum.elixirforum.com/t/migrations-on-fly-io-triggered-from-myapp-release-migrate-what-does-ash-migrate-do/72734 "2025-10-01T21:05:47Z")

</div>

Has anyone set up their Ash Framework Postgres app deployment (and, most importantly, migration) on Fly.io? I’m trying to figure out what I need to do for a safe deployment/migration. From what I’ve deciphered on the F…

---

## [How I create postgres unlogged tables with ecto migrations](https://forum.elixirforum.com/t/how-i-create-postgres-unlogged-tables-with-ecto-migrations/72391)

<div class="topic-metadata">

**Author:** [@mybadclan](https://forum.elixirforum.com/u/mybadclan)\
**Replies:** 1\
**Last updated:** [September 6, 2025, 10:27am UTC](https://forum.elixirforum.com/t/how-i-create-postgres-unlogged-tables-with-ecto-migrations/72391 "2025-09-06T10:27:59Z")

</div>

Hello there. How do I create an unlogged table in ecto migration when I use postgresql database?

---

## [Failed Ecto migration does not generate stack trace in app logs](https://forum.elixirforum.com/t/failed-ecto-migration-does-not-generate-stack-trace-in-app-logs/71665)

<div class="topic-metadata">

**Author:** [@lkananowicz](https://forum.elixirforum.com/u/lkananowicz)\
**Replies:** 3\
**Last updated:** [July 18, 2025, 12:15pm UTC](https://forum.elixirforum.com/t/failed-ecto-migration-does-not-generate-stack-trace-in-app-logs/71665 "2025-07-18T12:15:46Z")

</div>

Hi, I noticed strange problem in my Elixir app, whenever I run new migration on production env I get no feedback and the application starts despite the fact that migration actually failed. Checking app logs right after d…

---

## [Oban Pro 1.5.0 migration locked our production table](https://forum.elixirforum.com/t/oban-pro-1-5-0-migration-locked-our-production-table/70020)

<div class="topic-metadata">

**Author:** [@mrnovalles](https://forum.elixirforum.com/u/mrnovalles)\
**Replies:** 5\
**Last updated:** [July 10, 2025, 7:56pm UTC](https://forum.elixirforum.com/t/oban-pro-1-5-0-migration-locked-our-production-table/70020 "2025-07-10T19:56:27Z")

</div>

We’re proud and happy users of Oban.Pro , however in the most recent upgrade to v1.5.0, the migrations ended up locking the oban\_jobs table for over 10 mins. This caused our production app to fail on incoming requests. …

---

## [Multi-tenancy: Phx.gen.auth, Ecto.Migration.execute/1 specifying the schema](https://forum.elixirforum.com/t/multi-tenancy-phx-gen-auth-ecto-migration-execute-1-specifying-the-schema/70449)

<div class="topic-metadata">

**Author:** [@DidactMacros](https://forum.elixirforum.com/u/DidactMacros)\
**Replies:** 1\
**Last updated:** [April 20, 2025, 4:47pm UTC](https://forum.elixirforum.com/t/multi-tenancy-phx-gen-auth-ecto-migration-execute-1-specifying-the-schema/70449 "2025-04-20T16:47:59Z")

</div>

I’m currently handling auth on multi-tenancy, is this approach to migration warranted? TLWR; I’m schema targeting everything including the execute function that resolves case sensitivity issues. defmodule AuthTest.Repo.…

---

## [Seed data on a tenant](https://forum.elixirforum.com/t/seed-data-on-a-tenant/29924)

<div class="topic-metadata">

**Author:** [@franckstifler](https://forum.elixirforum.com/u/franckstifler)\
**Replies:** 2\
**Last updated:** [March 24, 2025, 4:12pm UTC](https://forum.elixirforum.com/t/seed-data-on-a-tenant/29924 "2025-03-24T16:12:51Z")

</div>

Hi, I’m building a multi-tenant app using prefixes. I wish to seed data on a given prefix using two different ways: using the command: mix run priv/repo/seeds.exs in the application on tenant creation. Everytime I run…

---

## [Ecto Migration Field Types](https://forum.elixirforum.com/t/ecto-migration-field-types/19825)

<div class="topic-metadata">

**Author:** [@fireproofsocks](https://forum.elixirforum.com/u/fireproofsocks)\
**Replies:** 18\
**Last updated:** [February 13, 2025, 3:19pm UTC](https://forum.elixirforum.com/t/ecto-migration-field-types/19825 "2025-02-13T15:19:38Z")

</div>

There is a nice table showing the available data types used in Ecto Schemas: Ecto.Schema — Ecto v3.14.0 But I cannot find a corresponding list that enumerates the possibilities available to an Ecto Migration. They are …

---

## [The --no-migration flag, does not prevent the generation of mirgrations when used with phx.gen.context](https://forum.elixirforum.com/t/the-no-migration-flag-does-not-prevent-the-generation-of-mirgrations-when-used-with-phx-gen-context/69328)

<div class="topic-metadata">

**Author:** [@DidactMacros](https://forum.elixirforum.com/u/DidactMacros)\
**Replies:** 4\
**Last updated:** [February 11, 2025, 1:56pm UTC](https://forum.elixirforum.com/t/the-no-migration-flag-does-not-prevent-the-generation-of-mirgrations-when-used-with-phx-gen-context/69328 "2025-02-11T13:56:18Z")

</div>

It is stated that the default gen settings can be overridden with use of appropriate options, Default options This generator uses default options provided in the :generators configuration of your application. These …

---

## [What is the best practice to run ecto migration before deploying?](https://forum.elixirforum.com/t/what-is-the-best-practice-to-run-ecto-migration-before-deploying/50427)

<div class="topic-metadata">

**Author:** [@nallwhy](https://forum.elixirforum.com/u/nallwhy)\
**Replies:** 9\
**Last updated:** [January 5, 2025, 12:48am UTC](https://forum.elixirforum.com/t/what-is-the-best-practice-to-run-ecto-migration-before-deploying/50427 "2025-01-05T00:48:19Z")

</div>

I’m deploying my app with fly.io. I have no idea how to run ecto migration before deploying. If I don’t do that, new version app will make errors because of broken DB schema. I only have the information about prod DB …

---

## [Maintaining materialized views with Ecto](https://forum.elixirforum.com/t/maintaining-materialized-views-with-ecto/67826)

<div class="topic-metadata">

**Author:** [@thiagomajesk](https://forum.elixirforum.com/u/thiagomajesk)\
**Replies:** 2\
**Last updated:** [December 2, 2024, 7:46pm UTC](https://forum.elixirforum.com/t/maintaining-materialized-views-with-ecto/67826 "2024-12-02T19:46:21Z")

</div>

Hi! :wave: I was thinking about this today and wanted to collect some feedback… I have been working with an OLAP database for the past year and using DBT for data transformation, and although it’s a great tool, having …

---

## [Renaming Postgres index, FK, PK, sequences](https://forum.elixirforum.com/t/renaming-postgres-index-fk-pk-sequences/67235)

<div class="topic-metadata">

**Author:** [@cblavier](https://forum.elixirforum.com/u/cblavier)\
**Replies:** 2\
**Last updated:** [November 4, 2024, 3:21pm UTC](https://forum.elixirforum.com/t/renaming-postgres-index-fk-pk-sequences/67235 "2024-11-04T15:21:31Z")

</div>

Hi everyone, I wanted to share some code that might be useful for those dealing with table renaming in Ecto migrations. While using Ecto.Migration.rename/2 is straightforward, we’ve encountered an issue: the associated …

---

## [Does Ecto.Migration.modify/3 allow removal of default values?](https://forum.elixirforum.com/t/does-ecto-migration-modify-3-allow-removal-of-default-values/23048)

<div class="topic-metadata">

**Author:** [@pinksynth](https://forum.elixirforum.com/u/pinksynth)\
**Replies:** 6\
**Last updated:** [August 28, 2024, 2:01pm UTC](https://forum.elixirforum.com/t/does-ecto-migration-modify-3-allow-removal-of-default-values/23048 "2024-08-28T14:01:22Z")

</div>

Hi all! First time posting, let me know if I can structure my question better. I am using Ecto migrations on a project and need to add columns with non-NULL constraints to a table with existing data. My down simply remo…

---

## [How can I specify the order of a composite primary key in an Ecto migration?](https://forum.elixirforum.com/t/how-can-i-specify-the-order-of-a-composite-primary-key-in-an-ecto-migration/65071)

<div class="topic-metadata">

**Author:** [@dli](https://forum.elixirforum.com/u/dli)\
**Replies:** 3\
**Last updated:** [July 22, 2024, 5:10pm UTC](https://forum.elixirforum.com/t/how-can-i-specify-the-order-of-a-composite-primary-key-in-an-ecto-migration/65071 "2024-07-22T17:10:15Z")

</div>

I am creating a schema where the order of the column definitions is relevant due to optimum memory alignment. The table needs to hold a very large number of rows, hence the additional pain here. When I add columns with p…

---

## [\#ecto #ecto-migrations Reversible \`references()\` migration with from: option](https://forum.elixirforum.com/t/ecto-ecto-migrations-reversible-references-migration-with-from-option/61711)

<div class="topic-metadata">

**Author:** [@1stSolo](https://forum.elixirforum.com/u/1stSolo)\
**Replies:** 5\
**Last updated:** [February 17, 2024, 3:05pm UTC](https://forum.elixirforum.com/t/ecto-ecto-migrations-reversible-references-migration-with-from-option/61711 "2024-02-17T15:05:10Z")

</div>

how do I make a reversible reference migration, i.e. adding the referential constraint on “up” action and dropping it on “down” action, using from: keyword? I’ve a pretty big migration and it’s much easier to use “def c…

---

## [Writing a new Ecto Adapter (not SQL), unable to use Ecto Migrations effectively](https://forum.elixirforum.com/t/writing-a-new-ecto-adapter-not-sql-unable-to-use-ecto-migrations-effectively/61673)

<div class="topic-metadata">

**Author:** [@jstimps](https://forum.elixirforum.com/u/jstimps)\
**Replies:** 3\
**Last updated:** [February 15, 2024, 2:17pm UTC](https://forum.elixirforum.com/t/writing-a-new-ecto-adapter-not-sql-unable-to-use-ecto-migrations-effectively/61673 "2024-02-15T14:17:56Z")

</div>

Hi, I’m working on an Ecto Adapter for FoundationDB and one of my goals is to support migrations in the same manner that Ecto users are accustomed to (e.g. mix ecto.migrate). As I familiarized myself with the adapter be…

---

## [Anybody know of an equivalent to Ruby's strong\_migrations gem?](https://forum.elixirforum.com/t/anybody-know-of-an-equivalent-to-rubys-strong-migrations-gem/38093)

<div class="topic-metadata">

**Author:** [@brightball](https://forum.elixirforum.com/u/brightball)\
**Replies:** 2\
**Last updated:** [February 10, 2024, 11:41pm UTC](https://forum.elixirforum.com/t/anybody-know-of-an-equivalent-to-rubys-strong-migrations-gem/38093 "2024-02-10T23:41:34Z")

</div>

I spent a lot of time on CI setup for a new project last week and went through all of the project staples to get things in a good place: sobelow, credo, deps.audit, dialyzer (with plt caching). But one of my favorite se…

---

## [Why does this code work as 2 separate migrations, but not as a single one?](https://forum.elixirforum.com/t/why-does-this-code-work-as-2-separate-migrations-but-not-as-a-single-one/61506)

<div class="topic-metadata">

**Author:** [@iarekk](https://forum.elixirforum.com/u/iarekk)\
**Replies:** 9\
**Last updated:** [February 7, 2024, 6:39pm UTC](https://forum.elixirforum.com/t/why-does-this-code-work-as-2-separate-migrations-but-not-as-a-single-one/61506 "2024-02-07T18:39:31Z")

</div>

Hi all, new to Elixir, going through the “Programming Phoenix LiveView” book right now. Fairly new to working with Ecto as well :slight\_smile: An exercise in chapter instructs the reader to add a username field to the U…

---

## [Ecto Migration: \`not\_null\_violation\` when modifying column null-\>non-null & adding default value](https://forum.elixirforum.com/t/ecto-migration-not-null-violation-when-modifying-column-null-non-null-adding-default-value/61448)

<div class="topic-metadata">

**Author:** [@spencer.christensen](https://forum.elixirforum.com/u/spencer.christensen)\
**Replies:** 5\
**Last updated:** [February 5, 2024, 3:48pm UTC](https://forum.elixirforum.com/t/ecto-migration-not-null-violation-when-modifying-column-null-non-null-adding-default-value/61448 "2024-02-05T15:48:15Z")

</div>

Howdy y’all, I’m brand new to Elixir and have what is likely a simple problem. I created an Ecto schema with a votes column that is type :integer and nullable. I later realized it should be non-nullable and default to 0…

---

## [How can I modify the on\_delete behaviour of a fk reference in a migration](https://forum.elixirforum.com/t/how-can-i-modify-the-on-delete-behaviour-of-a-fk-reference-in-a-migration/49209)

<div class="topic-metadata">

**Author:** [@benswift](https://forum.elixirforum.com/u/benswift)\
**Replies:** 6\
**Last updated:** [November 26, 2023, 10:50am UTC](https://forum.elixirforum.com/t/how-can-i-modify-the-on-delete-behaviour-of-a-fk-reference-in-a-migration/49209 "2023-11-26T10:50:51Z")

</div>

I created a table with a foreign key, and specified on\_delete: :nothing like so: create table(:foo) do add :bar\_id, references(:bar, on\_delete: :nothing) timestamps() end I now want to change the on\_delete behavio…

---

## [Squashing Schema Migrations](https://forum.elixirforum.com/t/squashing-schema-migrations/26184)

<div class="topic-metadata">

**Author:** [@fireproofsocks](https://forum.elixirforum.com/u/fireproofsocks)\
**Replies:** 28\
**Last updated:** [November 21, 2023, 2:13pm UTC](https://forum.elixirforum.com/t/squashing-schema-migrations/26184 "2023-11-21T14:13:06Z")

</div>

Anyone who has worked on an old code-base is familiar with this problem: the migrations folder can get huge. The database your app started with may bear little resemblance to the database it uses now. I’ve been reading…

---

## [How I can reorder migrations execution in umbrella apps?](https://forum.elixirforum.com/t/how-i-can-reorder-migrations-execution-in-umbrella-apps/59163)

<div class="topic-metadata">

**Author:** [@zoedsoupe](https://forum.elixirforum.com/u/zoedsoupe)\
**Replies:** 1\
**Last updated:** [October 21, 2023, 5:14pm UTC](https://forum.elixirforum.com/t/how-i-can-reorder-migrations-execution-in-umbrella-apps/59163 "2023-10-21T17:14:00Z")

</div>

I created an umbrella app as a template for a workshop I’ll give into my university. So I preferred to show an umbrella project to build many APIs projects that communicate between them. So each app has it own Ecto.Repo …

---

## [Upsert data records in a migration](https://forum.elixirforum.com/t/upsert-data-records-in-a-migration/58400)

<div class="topic-metadata">

**Author:** [@fireproofsocks](https://forum.elixirforum.com/u/fireproofsocks)\
**Replies:** 4\
**Last updated:** [September 20, 2023, 2:32pm UTC](https://forum.elixirforum.com/t/upsert-data-records-in-a-migration/58400 "2023-09-20T14:32:22Z")

</div>

In the process of migration between databases, I’m needing to do re-create some migrations that INSERT data. Let’s ignore the discussion/debate about whether or not data records should be handled inside a migration vs. a…

---

## [Load structure.sql as a migration?](https://forum.elixirforum.com/t/load-structure-sql-as-a-migration/58281)

<div class="topic-metadata">

**Author:** [@fireproofsocks](https://forum.elixirforum.com/u/fireproofsocks)\
**Replies:** 5\
**Last updated:** [September 14, 2023, 7:18pm UTC](https://forum.elixirforum.com/t/load-structure-sql-as-a-migration/58281 "2023-09-14T19:18:39Z")

</div>

I’m working on a fairly mature database and writing a new Elixir app to sit on top of it. The problem is getting the migrations started. The structure.sql is a great starting point, but we’d like to have it be part of …

---

## [Ecto create unique index with nulls for adjacency list](https://forum.elixirforum.com/t/ecto-create-unique-index-with-nulls-for-adjacency-list/57072)

<div class="topic-metadata">

**Author:** [@sharkmyster](https://forum.elixirforum.com/u/sharkmyster)\
**Replies:** 1\
**Last updated:** [July 14, 2023, 6:15pm UTC](https://forum.elixirforum.com/t/ecto-create-unique-index-with-nulls-for-adjacency-list/57072 "2023-07-14T18:15:29Z")

</div>

I’m trying to create a unique index with an ecto migration for a PostgresDB. def change do create table(:categories) do add(:name, :string) add(:parent\_id, references(:categories), null: true) timestamps(…

---

## [Manging migrations outside of Ecto](https://forum.elixirforum.com/t/manging-migrations-outside-of-ecto/56937)

<div class="topic-metadata">

**Author:** [@Rodeoclash](https://forum.elixirforum.com/u/Rodeoclash)\
**Replies:** 8\
**Last updated:** [July 9, 2023, 12:15pm UTC](https://forum.elixirforum.com/t/manging-migrations-outside-of-ecto/56937 "2023-07-09T12:15:39Z")

</div>

Has anyone had success with getting Ecto to use externally generated migration files? I have a directory of SQL files that I would like to migrate for my unit tests but loading each migration and executing it is causing …

---

## [Ash Primer: Migration Generator](https://forum.elixirforum.com/t/ash-primer-migration-generator/55368)

<div class="topic-metadata">

**Author:** [@zachdaniel](https://forum.elixirforum.com/u/zachdaniel)\
**Replies:** 1\
**Last updated:** [April 19, 2023, 2:22pm UTC](https://forum.elixirforum.com/t/ash-primer-migration-generator/55368 "2023-04-19T14:22:17Z")

</div>

The second video from the Ash Primers series is out! https://www.youtube.com/watch?v=GtsL\_lIis4Q This one is about the migration generator and how it can save time and keep important things where they belong! !:tada:

---

## [Ecto dont creates table but migration success](https://forum.elixirforum.com/t/ecto-dont-creates-table-but-migration-success/54116)

<div class="topic-metadata">

**Author:** [@zulkris](https://forum.elixirforum.com/u/zulkris)\
**Replies:** 3\
**Last updated:** [February 24, 2023, 10:17pm UTC](https://forum.elixirforum.com/t/ecto-dont-creates-table-but-migration-success/54116 "2023-02-24T22:17:28Z")

</div>

Hi all! for clarify i upload all to github to start: docker-compose up -d inside container (via docker exec -it app sh), im trying migrate via mix ecto.migrate migration “successfully” migrates: ❯ docker exec -it ap…

---

## [Redundant code in Ecto.Migration file - Phoenix Context Guide](https://forum.elixirforum.com/t/redundant-code-in-ecto-migration-file-phoenix-context-guide/52867)

<div class="topic-metadata">

**Author:** [@miguelszerman](https://forum.elixirforum.com/u/miguelszerman)\
**Replies:** 6\
**Last updated:** [December 31, 2022, 8:11pm UTC](https://forum.elixirforum.com/t/redundant-code-in-ecto-migration-file-phoenix-context-guide/52867 "2022-12-31T20:11:37Z")

</div>

In the context guide on Phoenix the following migration file is created for PostgresSQL: defmodule Hello.Repo.Migrations.CreateProductCategories do use Ecto.Migration def change do create table(:product\_categor…

---

## [Error when updating schema enum type](https://forum.elixirforum.com/t/error-when-updating-schema-enum-type/52189)

<div class="topic-metadata">

**Author:** [@grangerelixx](https://forum.elixirforum.com/u/grangerelixx)\
**Replies:** 0\
**Last updated:** [December 2, 2022, 10:42am UTC](https://forum.elixirforum.com/t/error-when-updating-schema-enum-type/52189 "2022-12-02T10:42:37Z")

</div>

Hi, I previously had the below migration and schema with statuses \[:start, :closed\]. def up do update\_statuses(\[:open, :closed\], :up) end def down do update\_statuses(\[:start, :closed\], :down) end defp…

[Next page](https://forum.elixirforum.com/tag/ecto-migration/1342.md?match_all_tags=true&page=1&tags%5B%5D=ecto-migration)
