tnederlof
I have a Phoenix app which is using ecto/postgres. I have setup two schemas (User has many Groups) and a seed file (contents below). I am running into an error when I add the group (which is supposed to have a user). I feel like the user association, where the constraint goes (requiring user_id vs assoc_constraint(:user)) and then how the seeding flows I am confused on.
Error I am getting
** (Ecto.InvalidChangesetError) could not perform insert because changeset is invalid.
Errors
%{user_id: [{"can't be blank", [validation: :required]}]}
Applied changes
%{description: "Random text", name: "Family"}
Params
%{
"description" => "Random text",
"name" => "Family",
"user" => %XXXX.Accounts.User{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
email: "example@gmail.com",
groups: #Ecto.Association.NotLoaded<association :groups is not loaded>,
id: 1,
inserted_at: ~N[2020-05-19 22:43:11],
name: "Example User",
password: "secret",
password_hash: "XXXXXXXX",
phonenumber: "1 203-247-7113",
updated_at: ~N[2020-05-19 22:43:11],
username: "exampleuser"
}
}
Changeset
#Ecto.Changeset<
action: :insert,
changes: %{description: "Random text", name: "Family"},
errors: [user_id: {"can't be blank", [validation: :required]}],
data: #XXXX.People.Group<>,
valid?: false
User Schema
defmodule XXXX.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
schema "users" do
field :email, :string
field :name, :string
field :password_hash, :string
field :password, :string, virtual: true
field :phonenumber, :string
field :username, :string
has_many :groups, XXXX.People.Group
timestamps()
end
def changeset(user, attrs) do
user
|> cast(attrs, [:name, :username, :email, :password, :phonenumber])
|> validate_required([:name, :username, :email, :password, :phonenumber])
|> validate_length(:username, min: 4)
|> validate_length(:password, min: 4)
|> unique_constraint(:username)
|> unique_constraint(:email)
|> unique_constraint(:phonenumber)
|> hash_password()
end
defp hash_password(changeset) do
case changeset do
%Ecto.Changeset{valid?: true, changes: %{password: password}} ->
put_change(changeset, :password_hash, Pbkdf2.hash_pwd_salt(password))
_ ->
changeset
end
end
end
User Migration
defmodule XXXX.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table(:users) do
add :name, :string, null: false
add :username, :string, null: false
add :email, :string, null: false
add :password_hash, :string, null: false
add :phonenumber, :string, null: false
timestamps()
end
create unique_index(:users, [:username, :email, :phonenumber])
end
end
Group Schema
defmodule XXXX.People.Group do
use Ecto.Schema
import Ecto.Changeset
schema "groups" do
field :description, :string
field :name, :string
belongs_to :user, XXXX.Accounts.User
timestamps()
end
def changeset(group, attrs) do
required_fields = [:name, :user_id]
optional_fields = [:description]
group
|> cast(attrs, required_fields ++ optional_fields)
|> validate_required(required_fields)
|> assoc_constraint(:user)
end
end
Group Migration
defmodule XXXX.Repo.Migrations.CreateGroups do
use Ecto.Migration
def change do
create table(:groups) do
add :name, :string, null: false
add :description, :text
add :user_id, references(:users, on_delete: :delete_all), null: false
timestamps()
end
end
end
seeds.exs
alias XXXX.Repo
alias XXXX.People.Group
alias XXXX.Accounts.User
user_example_1 = %User{} |> User.changeset(%{
name: "Example User",
username: "exampleuser",
phonenumber: "1 834-000-0000",
email: "example@gmail.com",
password: "secret"
}) |> Repo.insert!
example_group1 = %Group{} |> Group.changeset(%{
name: "Family",
description: "Random text",
user: user_example_1
}) |> Repo.insert!
Trending in Questions
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #elixirconf-us
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
patrickdm
Hello, I am not sure I understand you use case: with this schema each groups record is related to only one users record (groups record belong to a single user, via the user_id foreign key): so you can have many groups records related to the same user, but you can’t have multiple users in the same group. Then I’m not sure this is what you want to model with your schema. In my opinion it would make more sense to relate users and groups with a many-to-many relationship, so you can have a user belonging to multiple groups and a group containing many different users. If that is the case, you can find more details in the ecto docs https://hexdocs.pm/ecto/Ecto.Association.ManyToMany.html#content, else I apologize for misunderstanding your question.
tnederlof
Thanks for the response. I actually do want one user to have many groups. The groups are specific to each user and will be generated by the user. I really want to build a seed file passing the user struct to the creation of the group.
patrickdm
I see,
sorry for the misleading answer then!
kokolegorille
It is usually not a good idea to expose id field in the changeset.
You can do it in different ways, but if You have already a user, You could use put_assoc.
Another way to do it is to use build_assoc.
tnederlof
Thank you! Reading through put_assoc I learned a lot more about associations and changesets. I can see why exposing the id field to the changeset is a bad idea, I will stick with using put and build for now. I appreciate your help again and patience, I am learning a lot!