Value is invalid in multiple_select

I have a multiple_select to select a single medic for an appointment, I managed to show up a list to choose a medic, and when inspecting the element it does have an id as a value, the thing is that when trying to add an appointment it will give an error is invalid in the medic field.
This is my form:

<%= form_for @changeset, @action, fn f -> %>
  <%= if @changeset.action do %>
    <div class="alert alert-danger">
      <p>Oops, something went wrong! Please check the errors below.</p>
    </div>
  <% end %>

(...)

  <div class="form-group">
    <%= label f, :id_medico, class: "control-label" %>
    <%= multiple_select f, :id_medico, Enum.map(@medics, &{&1.nombre, Integer.to_string(&1.id)}) %>
    <%= error_tag f, :id_medico %>
  </div>

(...)

  <div class="form-group">
    <%= submit "âś”", class: "btn btn-primary" %>
    <a class="btn btn-danger" href="/appointments" role="button">âś–</a>
  </div>
<% end %>

and my controller:

  def new(conn, _params) do
    medics = Clinic.list_medics()
    changeset = Clinic.change_appointment(%Appointment{})
    render(conn, "new.html", changeset: changeset, medics: medics)
  end

Thanks.

I guess the problem is in the appointment schema.

This is the appointment schema:

schema "appointments" do
    field :bloque, :integer
    field :descripcion, :string
    field :fecha, :date
    field :id_medico, :integer
    field :rut_paciente, :string

    timestamps()
  end

… and the validation

Sorry:

def changeset(appointment, attrs) do
    appointment
    |> cast(attrs, [:rut_paciente, :fecha, :id_medico, :bloque, :descripcion])
    |> validate_required([:rut_paciente, :fecha, :id_medico, :bloque, :descripcion])
    |>unique_constraint(:bloque, name: :appointments_fecha_bloque_id_medico_index)
    |>unique_constraint(:fecha, name: :appointments_fecha_bloque_id_medico_index)
    |>unique_constraint(:id_medico, name: :appointments_fecha_bloque_id_medico_index)
    |>foreign_key_constraint(:bloque, name: :appointments_id_medico_fkey)
    |>foreign_key_constraint(:fecha, name: :appointments_id_medico_fkey)
    |>foreign_key_constraint(:id_medico, name: :appointments_id_medico_fkey)
  end

If I change it to number_input it works fine, but I would like to implement a multiple_select

It seems like has_many/belongs_to, but You do not define association.

I would add those… like this

schema "appointments" do
    field :bloque, :integer
    field :descripcion, :string
    field :fecha, :date
    field :rut_paciente, :string

    belongs_to :medico, Medico

    timestamps()
  end


schema "medicos" do
    ...
    has_many :appointments, Appointment
    timestamps()
  end

I also doubt about this, because a medico could potentially have may appointment, but here You say something else… You say there should be only one appointment with id_medico = x

|>unique_constraint(:id_medico, name: :appointments_fecha_bloque_id_medico_index)

Hmm, for some reason now I get:
ERROR 42703 (undefined_column): column a0.medics_id does not exist
In index.

Also, the unique constraint is there to avoid mutliple appointments with the same date, block and medic, the combination of these three attributes must be unique.

It looks like a typo… it should be medico_id

I re-made the project and now the medics table is called “medics”, so I don’t think it’s a typo

I am not sure of the way You set your constraints. There is a post here describing the process

https://medium.com/@ivorpaul/ecto-how-to-validate-unique-combinations-of-multiple-fields-8002c74c6757

I think You should have a unique index on multiple fields in the migration, then use only one constraint.

But it does not explain why it works for number_input and not for multiple select.

For this, I would add

IO.inspect(params)

to the create action of your controller, just to check what You receive as input…

I’m sorry, I’m unfamiliar with IO.inspect, I did google for a while and inserted into the create action, but I don’t know where should I see the output, also I’m thinking I should just remake the table with belongs_to and has_many and avoid using :id_medico as an attribute name.

The medics table is now plural, but what is the name of your Schema? Medic or Medics?

The schema is usually singular, and it can cause some trouble, in particular the way you will name association. You will use singular for has_one, belongs_to but plural for has_many, many_to_many

As I see your key, I guess You have defined a Medics schema, as a plural form.

IO.inspect logs to the running console, where You started the server…

This should be the output:

[debug] Processing with MedicaWeb.AppointmentController.create/2
  Parameters: %{"_csrf_token" => "NTwBMhoPAzsdXTEuFy47PVYBIDpyJgAADZbcHm2xp7kbzmZV/uZcBQ==", "_utf8" => "âś“", "appointment" => %{"bloque" => "1", "descripcion" => "1", "fecha" => "2018-06-06", "id_medico" => "1", "rut_paciente" => "19386399-K"}}                      
  Pipelines: [:browser]                                                                                                              
%{
  "bloque" => "1",
  "descripcion" => "1",
  "fecha" => "2018-06-06",
  "id_medico" => "1",
  "rut_paciente" => "19386399-K"
}

Also, the schema is called “medics” and the module is Medica.Clinic.Medic

If the module is called Medic, then there is something wrong…

It should never have requested for medics_id, but for medic_id.

And appointment should belongs_to medic (singular!) with medic_id as the key of this association.

If You have something belongs_to a user, the key will be user_id, and not users_id.

So I guess this sentence should be wrong?
mix phx.gen.html Clinic Medic medics nombre:string apellido:string especialidad:string

This command looks fine, but this message sure does not

Hmm, for some reason now I get:
ERROR 42703 (undefined_column): column a0.medics_id does not exist
In index.

Do You have this line in appointment?

belongs_to :medic, Clinic.Medic

Yes, tried with both medic and medics, medic returns (with Clinic.Medic):
ERROR 42703 (undefined_column): column a0.medic_id does not exist,
could it be because I used id_medico as an attribute (I removed it in appointment).