
conradwt
Programming Phoenix 1.4: Add category_id to video table (page 121)
On page 103, we perform:
mix phx.gen.html Multimedia Video videos user_id:references:users \
url:string title:string description:text
The above code adds a field, user_id
, to the videos
tables.
video.ex:
defmodule Rumbl.Multimedia.Video do
use Ecto.Schema
import Ecto.Changeset
schema "videos" do
field :description, :string
field :title, :string
field :url, :string
field :user_id, :id
belongs_to :user, Rumbl.Accounts.User
belongs_to :category, Rumbl.Multimedia.Category
timestamps()
end
@doc false
def changeset(video, attrs) do
video
|> cast(attrs, [:url, :title, :description, :category_id])
|> validate_required([:url, :title, :description])
end
end
One page 121, when I attempt to perform:
mix ecto.gen.migration add_category_id_to_video
I’m getting the following error message:
Compiling 1 file (.ex)
== Compilation error in file lib/rumbl/multimedia/video.ex ==
** (ArgumentError) field/association :user_id is already set on schema
(ecto 3.5.8) lib/ecto/schema.ex:2056: Ecto.Schema.put_struct_field/3
(ecto 3.5.8) lib/ecto/schema.ex:1809: Ecto.Schema.define_field/4
(ecto 3.5.8) lib/ecto/schema.ex:1896: Ecto.Schema.__belongs_to__/4
lib/rumbl/multimedia/video.ex:11: (module)
(stdlib 3.14) erl_eval.erl:680: :erl_eval.do_apply/6
(elixir 1.11.3) lib/kernel/parallel_compiler.ex:314: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7
Marked As Solved

conradwt
I was able to resolve the issue by removing user_id
field from the videos
schema. It would be great if the book was updated to show the error caused by having both
field :user_id, :id
and belongs_to :user, Rumbl.Accounts.User
and providing a resolution.
Popular Prag Prog topics

Hi everyone!
There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New

Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this book… ...
New

Hi @venkats,
It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New

I ran this command after installing the sample application:
$ cards add do something --owner Brian
And got a file not found error:
Fil...
New

I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint.
How to replicate:
load chapter-7 from zipfile i...
New

Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New

AWDWR 7, page 152, page 153:
Hello everyone,
I’m a little bit lost on the hotwire part. I didn’t fully understand it.
On page 152 @rub...
New

@mfazio23
I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
New

Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New

Getting an error when installing the dependencies at the start of this chapter:
could not compile dependency :exla, "mix compile" failed...
New
Other popular topics

A PragProg Hero’s Journey with Brian P. Hogan @bphogan
Have you ever worried that your only legacy will be in the form of legacy...
New

I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New

I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
New

Do the test and post your score :nerd_face:
:keyboard:
If possible, please add info such as the keyboard you’re using, the layout (Qw...
New

Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New

Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New

Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New

If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol:
bre...
New
New

Author Spotlight:
Peter Ullrich
@PJUllrich
Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /haskell
- /java
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /scala
- /zig
- /html
- /debian
- /nixos
- /lisp
- /agda
- /textmate
- /sublime-text
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /manjaro
- /django
- /spring
- /diversity
- /nodejs
- /lua
- /c
- /julia
- /slackware
- /neovim