J'apprends Elixir et je crée maintenant une application avec une table users
et une table user_informations
. J'essaye de demander user_id
dans user_informations
mais il n'a pas été inséré, et je ne comprends pas pourquoi parce que tous les autres champs ont été insérés.Clé étrangère non insérée dans la table ecto
est ici la table user_informations:
def change do
create table(:user_informations, primary_key: false) do
add :user_information_id, :uuid, primary_key: true
add :user_id, references(:users, [column: :user_id, type: :uuid])
add :full_name, :string
add :job_title, :string
add :address, :string
add :city, :string
add :country, :string
add :phone_number, :integer
add :bio, :string
timestamps()
end
create unique_index(:user_informations, [:user_information_id])
end
Le modèle UserInformation:
defmodule MyApp.Accounts.UserInformation do
use Ecto.Schema
alias MyApp.Accounts
import Ecto.Changeset
@primary_key {:user_information_id, Ecto.UUID, autogenerate: true}
@foreign_key_type Ecto.UUID
schema "user_informations" do
field :full_name, :string
field :job_title, :string
field :address, :string
field :city, :string
field :country, :string
field :phone_number, :integer
field :bio, :string
belongs_to :users, Accounts.User, foreign_key: :user_id
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def user_details_changeset(struct, params \\ %{}) do
struct
|> cast(params, [:full_name, :job_title, :address, :city, :country, :phone_number, :bio])
|> validate_required([:full_name])
|> validate_length(:full_name, min: 2)
end
end
La fonction insertion du changeset de user_informations
:
def update(conn, %{"user_information" => user_information_params}) do
current_user = Guardian.Plug.current_resource(conn)
user_information_changeset = Ecto.build_assoc(current_user, :user_informations,
full_name: user_information_params["full_name"],
job_title: user_information_params["job_title"],
address: user_information_params["address"],
city: user_information_params["city"],
country: user_information_params["country"],
phone_number: user_information_params[:phone_number],
bio: user_information_params["bio"])
Repo.insert(user_information_changeset)
conn
|> put_flash(:info, "Success!")
|> redirect(to: page_path(conn, :index))
end
Je pense que build_assoc devrait gérer cela automatiquement, il vous manque probablement quelque chose. – JustMichael
Vous avez raison, apparemment 'current_user = Guardian.Plug.current_resource (conn)' ne retourne pas le 'user_id' comme prévu par Ecto. Par conséquent, j'ai supprimé 'build_assoc' et implémenté le changeset comme'% UserInformation {user_id: current_user.user_id, ....} '. Maintenant, cela fonctionne et de la bonne manière. – Martin