I wrote a test code for getting data from postgresql.
defmodule DbServerWeb.UserSchemaTest do
use ExUnit.Case, async: true
use DbServer.DataCase
alias DbServer.Users
alias DbServer.Schema.User
describe "user schema test." do
@insert_params %{
user_id: "test_id",
user_name: "test_name",
user_email: "email@gmail.com",
user_password: "Password123?",
user_gender: 0,
user_bio: "Howdy?",
user_birthday: DateTime.utc_now()
}
test "crud test." do
assert {_, struct} = Users.create_user(@insert_params)
assert %User{} = user = Users.get_user(struct.user_id)
end
end
end
defmodule DbServer.Users do
@moduledoc """
The users context.
"""
import Ecto.Query, warn: false
alias DbServer.Repo
alias DbServer.Schema.User
def create_user(user \\ %{}) do
%User{}
|> User.changeset(user)
|> Repo.insert()
end
def get_user(id \\ :empty) do
Repo.get!(User, id)
end
end
defmodule DbServer.Repo.Migrations.Users 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
defmodule DbServer.Schema.Game do
use Ecto.Schema
import Ecto.Changeset
#@primary_key {:game_id, :id, autogenerate: true}
schema "games" do
field :game_name, :string
has_many :users, DbServer.Schema.User
has_one :tournaments_game, DbServer.Schema.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
But my terminal says
** (Postgrex.Error) ERROR 42703 (undefined_column) column u0.following_games_id does not exist
query: SELECT u0."user_id", u0."user_name", u0."user_email", u0."user_password", u0."user_gender", u0."user_bio", u0."user_birthday", u0."user_hosting_experience", u0."following_games_id", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."user_id" = $1)
hint: Perhaps you meant to reference the column "u0.following_games".
Why my ecto refer following_games_id not following_games?