@al2o3cr after doing this way add :user_id, references(:user, type: :string, on_delete: :nothing) and runs again ecto.migrate the following error occurs :
** (Postgrex.Error) ERROR 42804 (datatype_mismatch) foreign key constraint "user_session_user_id_fkey" cannot be implemented
Key columns "user_id" and "id" are of incompatible types: character varying and bigint.
(ecto_sql 3.8.3) lib/ecto/adapters/sql.ex:932: Ecto.Adapters.SQL.raise_sql_call_error/1
(elixir 1.14.2) lib/enum.ex:1658: Enum."-map/2-lists^map/1-0-"/2
(ecto_sql 3.8.3) lib/ecto/adapters/sql.ex:1024: Ecto.Adapters.SQL.execute_ddl/4
(ecto_sql 3.8.3) lib/ecto/migration/runner.ex:352: Ecto.Migration.Runner.log_and_execute_ddl/3
(ecto_sql 3.8.3) lib/ecto/migration/runner.ex:117: anonymous fn/6 in Ecto.Migration.Runner.flush/0
(elixir 1.14.2) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto_sql 3.8.3) lib/ecto/migration/runner.ex:116: Ecto.Migration.Runner.flush/0
(ecto_sql 3.8.3) lib/ecto/migration/runner.ex:289: Ecto.Migration.Runner.perform_operation/3
(stdlib 4.0.1) timer.erl:235: :timer.tc/1
(ecto_sql 3.8.3) lib/ecto/migration/runner.ex:25: Ecto.Migration.Runner.run/8
(ecto_sql 3.8.3) lib/ecto/migrator.ex:348: Ecto.Migrator.attempt/8
(ecto_sql 3.8.3) lib/ecto/migrator.ex:263: anonymous fn/5 in Ecto.Migrator.do_up/5
(ecto_sql 3.8.3) lib/ecto/migrator.ex:319: anonymous fn/6 in Ecto.Migrator.async_migrate_maybe_in_transaction/7
(ecto_sql 3.8.3) lib/ecto/adapters/sql.ex:1222: anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4
(db_connection 2.4.3) lib/db_connection.ex:1611: DBConnection.run_transaction/4
(ecto_sql 3.8.3) lib/ecto/migrator.ex:337: Ecto.Migrator.run_maybe_in_transaction/5
(elixir 1.14.2) lib/task/supervised.ex:89: Task.Supervised.invoke_mfa/2
(elixir 1.14.2) lib/task/supervised.ex:34: Task.Supervised.reply/4
Here is the code to add user_id :
defmodule Dbservice.Repo.Migrations.AddUserIdInUserSession do
use Ecto.Migration
def change do
alter table("user_session") do
add :user_id, :string
end
end
end
Here is the user_session_view
defmodule DbserviceWeb.UserSessionView do
use DbserviceWeb, :view
alias DbserviceWeb.UserSessionView
def render("index.json", %{user_session: user_session}) do
render_many(user_session, UserSessionView, "user_session.json")
end
def render("show.json", %{user_session: user_session}) do
render_one(user_session, UserSessionView, "user_session.json")
end
def render("user_session.json", %{user_session: user_session}) do
%{
id: user_session.id,
start_time: user_session.start_time,
end_time: user_session.end_time,
session_occurrence_id: user_session.session_occurrence_id,
data: user_session.data,
is_user_valid: user_session.is_user_valid,
user_id: user_session.user_id
}
end
end
From these code can you help to get any insight how can I resolve my error ?






















