theshapguy
Ecto Many to Many Preload: Serial Type Not Found
defmodule App.Newsletters.Newsletter do
use App.Schema
import Ecto.Changeset
schema "newsletters" do
field :frequency, :string
field :name, :string
field :sender_name, :string
field :email, :string
field :selected?, :boolean, virtual: true, default: false
timestamps()
end
@doc false
def changeset(newsletter, attrs) do
newsletter
|> cast(attrs, [:name, :frequency, :sender_name])
|> validate_required([:name, :frequency, :sender_name])
end
end
defmodule App.Digests.Digest do
use App.Schema
import Ecto.Changeset
schema "digests" do
field :frequency, :string
field :name, :string
belongs_to :organization, App.Organizations.Organization
many_to_many(
:newsletters,
App.Newsletters.Newsletter,
join_through: App.Digests.DigestsNewsletter,
on_replace: :delete,
join_keys: [digest_id: :id, newsletter_id: :id]
)
timestamps()
end
def for_id(query \\ __MODULE__, id) do
query
|> where([s], s.id == ^id)
end
@doc false
def changeset(digest, attrs) do
digest
|> cast(attrs, [:name, :frequency])
|> validate_required([:name, :frequency])
end
end
defmodule App.Digests.DigestsNewsletter do
@moduledoc """
UserProject module
"""
use App.Schema
import Ecto.Changeset
alias App.Digests.Digest
alias App.Newsletters.Newsletter
@already_exists "ALREADY_EXISTS"
@primary_key false
schema "newsletters_digests" do
belongs_to(:digest, Digest, primary_key: true)
belongs_to(:newsletter, Newsletter, primary_key: true)
timestamps()
end
@required_fields ~w(newsletter_id user_id)a
def changeset(newsletters_digests, params \\ %{}) do
newsletters_digests
|> cast(params, @required_fields)
|> validate_required(@required_fields)
|> foreign_key_constraint(:digest_id)
|> foreign_key_constraint(:newsletter_id)
|> unique_constraint([:digest, :newsletter],
name: :digest_id_newsletter_id_unique_index,
message: @already_exists
)
end
end
## Many to Many Migration
defmodule App.Repo.Migrations.CreateUserNewsletters do
use Ecto.Migration
def change do
create table(:newsletters_digests, primary_key: false) do
add(:digest_id, references(:digests, on_delete: :delete_all), primary_key: true)
add(:newsletter_id, references(:newsletters, on_delete: :delete_all), primary_key: true)
timestamps()
end
create(index(:newsletters_digests, [:digest_id]))
create(index(:newsletters_digests, [:newsletter_id]))
create(
unique_index(:newsletters_digests, [:digest_id, :newsletter_id],
name: :digest_id_newsletter_id_unique_index
)
)
end
end
I have a many to many field that connects newsletters and digests. When I try to preload the many to many field newsletters via
Repo.get!(Digest, id)
|> Repo.preload(:newsletters)
I get the following error
[error] #PID<0.898.0> running AppWeb.Endpoint (connection #PID<0.880.0>, stream id 3) terminated
Server: localhost:4000 (http)
Request: GET /app/digest_live/mvMl2V
** (exit) an exception was raised:
** (Postgrex.Error) ERROR 42704 (undefined_object) type "serial" does not exist
query: SELECT n0."id", n0."frequency", n0."name", n0."sender_name", n0."email", n0."inserted_at", n0."updated_at", n1."digest_id"::serial FROM "newsletters" AS n0 INNER JOIN "newsletters_digests" AS n1 ON n0."id" = n1."newsletter_id" WHERE (n1."digest_id" = ANY($1)) ORDER BY n1."digest_id"::serial
(ecto_sql 3.8.0) lib/ecto/adapters/sql.ex:928: Ecto.Adapters.SQL.raise_sql_call_error/1
(ecto_sql 3.8.0) lib/ecto/adapters/sql.ex:843: Ecto.Adapters.SQL.execute/6
(ecto 3.8.1) lib/ecto/repo/queryable.ex:221: Ecto.Repo.Queryable.execute/4
(ecto 3.8.1) lib/ecto/repo/queryable.ex:19: Ecto.Repo.Queryable.all/3
(elixir 1.13.3) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
(app 0.1.0) lib/app_web/live/digest_live.ex:23: AppWeb.DigestLive.apply_action/3
(app 0.1.0) lib/app_web/live/digest_live.ex:19: AppWeb.DigestLive.handle_params/3
(phoenix_live_view 0.17.6) lib/phoenix_live_view/utils.ex:369: anonymous fn/5 in Phoenix.LiveView.Utils.call_handle_params!/5
(telemetry 1.1.0) /Users/shapathneupane/Developer/boilerplate_1.6/synopsis/deps/telemetry/src/telemetry.erl:320: :telemetry.span/3
(phoenix_live_view 0.17.6) lib/phoenix_live_view/static.ex:271: Phoenix.LiveView.Static.call_mount_and_handle_params!/5
(phoenix_live_view 0.17.6) lib/phoenix_live_view/static.ex:110: Phoenix.LiveView.Static.render/3
(phoenix_live_view 0.17.6) lib/phoenix_live_view/controller.ex:39: Phoenix.LiveView.Controller.live_render/3
(phoenix 1.6.6) lib/phoenix/router.ex:355: Phoenix.Router.__call__/2
(app 0.1.0) lib/app_web/endpoint.ex:1: AppWeb.Endpoint.plug_builder_call/2
(app 0.1.0) lib/plug/debugger.ex:136: AppWeb.Endpoint."call (overridable 3)"/2
(app 0.1.0) lib/app_web/endpoint.ex:1: AppWeb.Endpoint.call/2
However, when I try to preload :organization in the digest schema. It works and preloads the organization.
# this preloads organization without errors
Repo.get!(Digest, id)
|> Repo.preload(:organiaztion)
%App.Digests.Digest{
__meta__: #Ecto.Schema.Metadata<:loaded, "digests">,
frequency: "ONCE",
id: "mvMl2V",
inserted_at: ~U[2022-04-28 14:15:16Z],
name: "ANOTHER DATASET",
newsletters: #Ecto.Association.NotLoaded<association :newsletters is not loaded>,
organization: %App.Organizations.Organization{
__meta__: #Ecto.Schema.Metadata<:loaded, "organizations">,
active: true,
domain: nil,
email: nil,
id: 1,
inserted_at: ~U[2022-04-28 03:01:21Z],
},
organization_id: "gKg12n",
updated_at: ~U[2022-04-28 14:15:16Z]
}
Is there any changes to be made for many_to_many association preload?
Trending in Questions
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
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
- #code-sync
- #javascript
- #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
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









