Can't Store a date in a SQL BDD (Phoenix

Hi,

I’m trying to store some data in a bdd using POST method with postman but when it is about date I did not succeed.

This is my code :

 schema "workingtimes" do
    field :end, :utc_datetime, null: false
    field :start, :utc_datetime, null: false
    belongs_to :user, App.Result.User

    timestamps()
  end

  @doc false
  def changeset(workingtime, attrs) do
    workingtime
    |> cast(attrs, [:start, :end])
    |> validate_required([:start, :end, :user_id])
  end
end

I don’t think it’s my SQL BDD fault because I can store string easily but not some date…

{ "workingtim": { "start": "2018-09-22 08:24:22", "end": "2018-10-22 08:24:22" } }

Saddly I have to use this format “YYYY-MM-DD HH:MM:SS” to upload dates with post method

I’m New to elixir so sorry if there is some big mistakes, and sry for my bad english

Thank you very much !

This is the error I get from my local server

Request: POST /api/workingtimes
** (exit) an exception was raised:
    ** (RuntimeError) casting assocs with cast/4 for :user field is not supported, use cast_assoc/3 instead
        (ecto) lib/ecto/changeset.ex:545: Ecto.Changeset.type!/2
        (ecto) lib/ecto/changeset.ex:519: Ecto.Changeset.process_param/7
        (elixir) lib/enum.ex:1948: Enum."-reduce/3-lists^foldl/2-0-"/3
        (ecto) lib/ecto/changeset.ex:504: Ecto.Changeset.cast/6
        (timeManager) lib/timeManager/result/workingtime.ex:16: App.Result.Workingtime.changeset/2
        (timeManager) lib/timeManager/result.ex:246: App.Result.create_workingtime/1
        (timeManager) lib/timeManager_web/controllers/workingtime_controller.ex:15: AppWeb.WorkingtimeController.create/2
        (timeManager) lib/timeManager_web/controllers/workingtime_controller.ex:1: AppWeb.WorkingtimeController.action/2
        (timeManager) lib/timeManager_web/controllers/workingtime_controller.ex:1: AppWeb.WorkingtimeController.phoenix_controller_pipeline/2
        (phoenix) lib/phoenix/router.ex:288: Phoenix.Router.__call__/2
        (timeManager) lib/timeManager_web/endpoint.ex:1: AppWeb.Endpoint.plug_builder_call/2
        (timeManager) lib/plug/debugger.ex:122: AppWeb.Endpoint."call (overridable 3)"/2
        (timeManager) lib/timeManager_web/endpoint.ex:1: AppWeb.Endpoint.call/2
        (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:42: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy) c:/Users/Nightingale/Desktop/TimeManager/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
        (cowboy) c:/Users/Nightingale/Desktop/TimeManager/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3
        (cowboy) c:/Users/Nightingale/Desktop/TimeManager/deps/cowboy/src/cowboy_stream_h.erl:274: :cowboy_stream_h.request_process/3
        (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

Hello and welcome,
You don’t cast user_id, so it will not work until You add it to this list

1 Like

Thanks for the answer so I add user_id in the cast but now when I post the same thing this is my error

[info] POST /api/workingtimes
[debug] Processing with AppWeb.WorkingtimeController.create/2
  Parameters: %{"workingtime" => %{"end" => "2019-04-03 08:08:28", "start" => "2019-03-03 08:08:28", "user_id" => "1"}}
  Pipelines: [:api]
[info] Sent 500 in 31ms
[error] #PID<0.753.0> running AppWeb.Endpoint (connection #PID<0.752.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: POST /api/workingtimes
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in Ecto.Changeset.cast_assoc/3
        (ecto) lib/ecto/changeset.ex:718: Ecto.Changeset.cast_assoc(#Ecto.Changeset<action: nil, changes: %{end: ~U[2019-04-03 08:08:28Z], start: ~U[2019-03-03 08:08:28Z], user_id: 1}, errors: [], data: #App.Result.Workingtime<>, valid?: true>, [:start, :end, :user_id], [])
        (timeManager) lib/timeManager/result/workingtime.ex:17: App.Result.Workingtime.changeset/2
        (timeManager) lib/timeManager/result.ex:246: App.Result.create_workingtime/1
        (timeManager) lib/timeManager_web/controllers/workingtime_controller.ex:15: AppWeb.WorkingtimeController.create/2
        (timeManager) lib/timeManager_web/controllers/workingtime_controller.ex:1: AppWeb.WorkingtimeController.action/2
        (timeManager) lib/timeManager_web/controllers/workingtime_controller.ex:1: AppWeb.WorkingtimeController.phoenix_controller_pipeline/2
        (phoenix) lib/phoenix/router.ex:288: Phoenix.Router.__call__/2
        (timeManager) lib/timeManager_web/endpoint.ex:1: AppWeb.Endpoint.plug_builder_call/2
        (timeManager) lib/plug/debugger.ex:122: AppWeb.Endpoint."call (overridable 3)"/2
        (timeManager) lib/timeManager_web/endpoint.ex:1: AppWeb.Endpoint.call/2
        (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:42: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy) c:/Users/Nightingale/Desktop/TimeManager/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
        (cowboy) c:/Users/Nightingale/Desktop/TimeManager/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3
        (cowboy) c:/Users/Nightingale/Desktop/TimeManager/deps/cowboy/src/cowboy_stream_h.erl:274: :cowboy_stream_h.request_process/3
        (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

Your code does not show the part in error, most probably, it’s here

lib/timeManager/result/workingtime.ex:17

And the main error is

casting assocs with cast/4 for :user field is not supported, use cast_assoc/3 instead

I suspect You are passing a user instead of user_id, but without more info, it’s hard to tell

I’m sry because I said it wasn’t an error in my SQL BDD but it was i change the columns in timestanp without time zone and it worked perfectly thanks again for all ur help !!