maz
Ecto adds plural ending to foreign key reference, causing runtime error
Running ecto 3.6.2 and in seeds.exs I’m simply updating every mediaitem with a timestamp and when ecto does the SELECT, it selects a non-existent column channels_id:
# set all seeded mediaitems to be publishable
FaithfulWord.Repo.all(from m in MediaItem)
|> Enum.map(fn mediaitem ->
mediaitem
|> change(published_at: DateTime.utc_now())
|> Repo.update()
end)
error (“* (Postgrex.Error) ERROR 42703 (undefined_column) column m0.channels_id does not exist”):
[debug] QUERY ERROR source="mediaitems" db=0.0ms queue=10.1ms idle=14.9ms
SELECT m0."id", m0."comment", m0."description", m0."dislike_count", m0."size_bytes", m0."filepath", m0."height", m0."like_count", m0."tags", m0."title", m0."filename_hash", m0."upload_date", m0."artist", m0."view_count", m0."webpage_url", m0."width", m0."media_format", m0."skylink_id", m0."language_id", m0."localizedname", m0."ordinal", m0."cdn_path", m0."presenter_name", m0."presented_at", m0."source_material", m0."track_number", m0."media_category", m0."playlist_id", m0."org_id", m0."owner_id", m0."published_at", m0."readable_id", m0."duration", m0."channels_id", m0."playlists_id", m0."inserted_at", m0."updated_at" FROM "mediaitems" AS m0 []
** (Postgrex.Error) ERROR 42703 (undefined_column) column m0.channels_id does not exist
query: SELECT m0."id", m0."comment", m0."description", m0."dislike_count", m0."size_bytes", m0."filepath", m0."height", m0."like_count", m0."tags", m0."title", m0."filename_hash", m0."upload_date", m0."artist", m0."view_count", m0."webpage_url", m0."width", m0."media_format", m0."skylink_id", m0."language_id", m0."localizedname", m0."ordinal", m0."cdn_path", m0."presenter_name", m0."presented_at", m0."source_material", m0."track_number", m0."media_category", m0."playlist_id", m0."org_id", m0."owner_id", m0."published_at", m0."readable_id", m0."duration", m0."channels_id", m0."playlists_id", m0."inserted_at", m0."updated_at" FROM "mediaitems" AS m0
hint: Perhaps you meant to reference the column "m0.channel_id".
(ecto_sql 3.6.2) lib/ecto/adapters/sql.ex:760: Ecto.Adapters.SQL.raise_sql_call_error/1
(ecto_sql 3.6.2) lib/ecto/adapters/sql.ex:693: Ecto.Adapters.SQL.execute/5
(ecto 3.6.2) lib/ecto/repo/queryable.ex:224: Ecto.Repo.Queryable.execute/4
(ecto 3.6.2) lib/ecto/repo/queryable.ex:19: Ecto.Repo.Queryable.all/3
priv/repo/seeds.exs:105: (file)
(elixir 1.11.1) lib/code.ex:931: Code.require_file/2
(mix 1.11.1) lib/mix/tasks/run.ex:146: Mix.Tasks.Run.run/5
(mix 1.11.1) lib/mix/tasks/run.ex:86: Mix.Tasks.Run.run/1
(mix 1.11.1) lib/mix/task.ex:394: Mix.Task.run_task/3
(mix 1.11.1) lib/mix/cli.ex:84: Mix.CLI.run_task/2
(elixir 1.11.1) lib/code.ex:931: Code.require_file/2
This was after I added a new column to MediaItem that I call channel_id:
defmodule FaithfulWord.Schema.MediaItem do
use Ecto.Schema
import Ecto.Changeset
...
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "mediaitems" do
...
field :playlist_id, :binary_id
field :channel_id, :binary_id
...
belongs_to :channels, FaithfulWord.Schema.Channel
belongs_to :playlists, FaithfulWord.Schema.Playlist
timestamps(type: :utc_datetime_usec)
end
...
migration:
defmodule FaithfulWord.Repo.Migrations.CreateMediaItems do
use Ecto.Migration
def change do
create table(:mediaitems, primary_key: false) do
add :id, :binary_id, primary_key: true
...
add :org_id, references(:orgs, on_delete: :delete_all, type: :binary_id)
add :owner_id, references(:users, on_delete: :delete_all, type: :binary_id)
add :channel_id, references(:channels, on_delete: :delete_all, type: :binary_id)
add :playlist_id, references(:playlists, on_delete: :delete_all, type: :binary_id) timestamps(type: :utc_datetime_usec)
...
end
...
Any idea ecto seems to add a pluralized version(“channels_id”) of the actual column(“channel_id”)? Is there a workaround?
Michael
Marked As Solved
kokolegorille
Isn’t it duplication?
When using belongs_to, it should not be necessary to define foreign_key fields
1
Popular in Questions
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> somethi...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
I have a super simple question about elixir - how would I take a file like this
foo
bar
baz
and output a new file that enumerates th...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
Other popular topics
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition)
It’s been a while since we first asked this, I...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








