Papillon6814
Invalid association warning
I’m new to creating Database with ecto.
I created migration files and schema filed in a phoenix project.
These are my migration files and schema files.
migration files
defmodule DbServer.Repo.Migrations.CreateGames do
use Ecto.Migration
def change do
create table(:games) do
add :game_name, :string
timestamps()
end
end
end
defmodule DbServer.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table(:users, primary_key: false) do
add :user_id, :string, primary_key: true
add :user_name, :string
add :user_email, :string
add :user_password, :string
add :user_gender, :integer
add :user_bio, :string
add :user_birthday, :utc_datetime
add :user_hosting_experience, :integer
add :following_games_id, references(:games)
timestamps()
end
end
end
schema files
defmodule DbServer.Schema.Game do
use DbServer.AroundSchema
schema "games" do
field :game_name, :string
many_to_many :users, User, join_through: "users"
has_one :tournaments_game, Tournament
timestamps()
end
def changeset(game, params \\ :empty) do
game
|> cast(params, [:game_name])
|> validate_required(:game_name)
|> unique_constraint(:game_name, message: "The game already exists.")
end
end
defmodule DbServer.Schema.User do
use DbServer.AroundSchema
alias Comeonin.Bcrypt
@primary_key {:user_id, :string, []}
schema "users" do
field :user_name, :string
field :user_email, :string
field :user_password, :string
field :user_gender, :integer
field :user_bio, :string
field :user_birthday, :utc_datetime
field :user_hosting_experience, :integer, default: 0
#belongs_to :following_games, DbServer.Schema.Game
has_one :tournament, Tournament
timestamps()
end
@user_name_regex ~r"^[a-z0-9_\-\.]+$"
@email_regex ~r/@/
@doc false
def changeset(user, params \\ :empty) do
user
|> cast(params, [:user_id, :user_name, :user_email, :user_password, :user_gender, :user_bio, :user_birthday, :user_hosting_experience])
|> validate_required([:user_id, :user_name, :user_email, :user_password, :user_gender, :user_bio, :user_birthday, :user_hosting_experience])
|> unique_constraint(:user_id, message: "The id has been already taken.")
|> validate_length(:user_name, min: 3)
|> validate_format(:user_name, @user_name_regex)
|> unique_constraint(:user_email, message: "The email already exists.")
|> validate_format(:user_email, @email_regex, message: "Invalid format.")
|> validate_length(:user_password, min: 8, max: 20)
|> validate_format(:user_password, ~r/[A-Z]+/, message: "Password must contain an upper-case letter.")
|> validate_format(:user_password, ~r/[a-z]+/, message: "Password must contain a lower-case letter.")
|> validate_format(:user_password, ~r/[0-9]+/, message: "Password must contain a number.")
|> validate_format(:user_password, ~r/[#\!\?&@\$%^&*\(\)]+/, message: "Password must contain a symbol.")
|> validate_confirmation(:user_password, message: "Does not match password.")
|> put_pass_hash()
|> validate_length(:user_bio, max: 125)
end
defp put_pass_hash(%Ecto.Changeset{valid?: true, changes: %{password: password}} = changeset) do
change(changeset, user_password: Bcrypt.hashpwsalt(password))
end
defp put_pass_hash(changeset), do: changeset
end
In the unit tests, a warning message appeard.
warning: invalid association `tournament` in schema DbServer.Schema.User: associated schema DbServer.Schema.Tournament does not have field `user_user_id`
lib/db_server/schema/user.ex:1: DbServer.Schema.User (module)
.........
Finished in 0.1 seconds
9 tests, 0 failures
How can I resolve the warning?
Marked As Solved
benwilson512
Author of Craft GraphQL APIs in Elixir with Absinthe
Also Liked
benwilson512
Author of Craft GraphQL APIs in Elixir with Absinthe
Can I ask why you are prefixing all of your column names with user_ ? They’re already on a table named users. users.user_whatever just seems repetitive.
Setting that aside, the error explains the reason. You say that has_one :tournaments_game, Tournament in your User schema, this requires that the tournaments table has a column that points to the users table. By default that column would be named #{schema_name}_#{primary_key} which for you is user_user_id.
1
Popular in Questions
How to bind a phoenix app to a specific ip address?
could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
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 read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Other popular topics
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
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, 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
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









