dgreiss
Hi everyone,
I’m building out a complex database schema as part of a Phoenix app and I’m running into an error when i make updates to the migration. For example I have a :users table and :comments:
create table(:comments) do
add :title, :string
timestamps()
end
create table(:users) do
add :name, :string
timestamps()
end
I run ecto.migrate with no issue, but now I’d like to add a reference on the :comments table:
create table(:comments) do
add :title, :string
add :users_id, references(:users, on_delete: :nothing)
timestamps()
end
Rather than rolling back the previous migration I prefer to ecto.reset, however I encounter this error.
The database for MigrationExample.Repo has been dropped
The database for MigrationExample.Repo has been created
20:42:28.605 [info] == Running 20200126013640 MigrationExample.Repo.Migrations.CreateComments.change/0 forward
20:42:28.607 [info] create table comments
** (Postgrex.Error) ERROR 42P01 (undefined_table) relation "users" does not exist
(ecto_sql) lib/ecto/adapters/sql.ex:629: Ecto.Adapters.SQL.raise_sql_call_error/1
(elixir) lib/enum.ex:1336: Enum."-map/2-lists^map/1-0-"/2
(ecto_sql) lib/ecto/adapters/sql.ex:716: Ecto.Adapters.SQL.execute_ddl/4
(ecto_sql) lib/ecto/migration/runner.ex:343: Ecto.Migration.Runner.log_and_execute_ddl/3
(ecto_sql) lib/ecto/migration/runner.ex:117: anonymous fn/6 in Ecto.Migration.Runner.flush/0
(elixir) lib/enum.ex:1948: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto_sql) lib/ecto/migration/runner.ex:116: Ecto.Migration.Runner.flush/0
(stdlib) timer.erl:166: :timer.tc/1
(ecto_sql) lib/ecto/migration/runner.ex:25: Ecto.Migration.Runner.run/7
(ecto_sql) lib/ecto/migrator.ex:342: Ecto.Migrator.attempt/7
(ecto_sql) lib/ecto/migrator.ex:243: anonymous fn/4 in Ecto.Migrator.do_up/4
(ecto_sql) lib/ecto/migrator.ex:324: anonymous fn/3 in Ecto.Migrator.run_maybe_in_transaction/6
(ecto_sql) lib/ecto/adapters/sql.ex:903: anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4
(db_connection) lib/db_connection.ex:1427: DBConnection.run_transaction/4
(ecto_sql) lib/ecto/migrator.ex:323: Ecto.Migrator.run_maybe_in_transaction/6
(elixir) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
(elixir) lib/task/supervised.ex:35: Task.Supervised.reply/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
The issue is that the order of the migrations is important and the :comments table is being created before the :users table, and therefore throwing the error. Is there a way to resolve this without manually specifying the repo in the migration ecto.migrate -r or rolling back a migration. My preference is to always reset the database.
Thanks for the help
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
wolfiton
Hi
You need to create a new migration that will alter the comments table in your case and add the user_id refeference.
See here Ecto.Migration — Ecto SQL v3.14.0
You will have something like this
Then your migration will work as expected.
benwilson512
You need to create the users table before the comments table, your migration shows comments as first.
dgreiss
Is there a way to do specify the order of the migrations?
wolfiton
From what i experience so far the migration each get a timestamp so the order is the one you define. But using my method to add the relationships after creating the actual migrations should help you automatize the process.
dimitarvp
Yes, they are named with full timestamp (year, month, date, hour, minute, second) when created. If they are out of order then it means you created the comments migration first and then the users. You can simply rename your users migration to an earlier timestamp than that of the comments.
Check your
priv/repo/migrations/directory and you’ll see.After you edit the filenames then just drop your DB (
mix ecto.drop) and re-run migrations.LostKobrakai
They‘re applied in order of filenames. Generally the leading timestamps are used to link the alphabetical order to order of file creation.
dgreiss
I’ve settled on altering the table as suggested by @wolfiton and reserve altering the table for creating any references and indices. I don’t like the idea of changing the timestamps on the file names, especially if the file has been committed.