trenpixster / addict

User management lib for Phoenix Framework
MIT License
645 stars 99 forks source link

Compatibility with timestamp macro #100

Closed dianjin closed 8 years ago

dianjin commented 8 years ago

Hey, I'm new to Phoenix so I'm unsure if this is an error on my end or Addict's end. Anyway, I'm using the timestamps macro to add inserted_at and updated_at columns in my User model. When I tried to register a user though, I got the following error:

[info] GET /register
[debug] Processing by Addict.AddictController.register/2
  Parameters: %{}
  Pipelines: []
[info] Sent 200 in 33ms
[info] POST /register
[debug] Processing by Addict.AddictController.register/2
  Parameters: %{"email" => "<EMAIL HIDDEN BY ME>", "name" => "<HIDDEN BY ME>", "password" => "[FILTERED]"}
  Pipelines: []
[debug] INSERT INTO "users" ("email", "encrypted_password", "inserted_at", "name", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" ["<EMAIL HIDDEN BY ME>", "<HIDDEN BY ME>", nil, "<HIDDEN BY ME>", nil] ERROR query=81.4ms queue=5.5ms
[error] #PID<0.386.0> running Blog.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /register
** (exit) an exception was raised:
    ** (Postgrex.Error) ERROR (not_null_violation): null value in column "inserted_at" violates not-null constraint
        (ecto) lib/ecto/adapters/sql.ex:493: Ecto.Adapters.SQL.model/6
        (ecto) lib/ecto/repo/model.ex:253: Ecto.Repo.Model.apply/4
        (ecto) lib/ecto/repo/model.ex:83: anonymous fn/10 in Ecto.Repo.Model.do_insert/4
        lib/addict/interactors/register.ex:25: Addict.Interactors.Register.do_register/2
        lib/addict/controller.ex:16: Addict.AddictController.register/2
        lib/addict/controller.ex:1: Addict.AddictController.action/2
        lib/addict/controller.ex:1: Addict.AddictController.phoenix_controller_pipeline/2
        (blog) lib/blog/endpoint.ex:1: Blog.Endpoint.instrument/4
        (blog) lib/phoenix/router.ex:261: Blog.Router.dispatch/2
        (blog) web/router.ex:1: Blog.Router.do_call/2
        (blog) lib/blog/endpoint.ex:1: Blog.Endpoint.phoenix_pipeline/1
        (blog) lib/plug/debugger.ex:93: Blog.Endpoint."call (overridable 3)"/2
        (blog) lib/blog/endpoint.ex:1: Blog.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

So it looks like the inserted_at and updated_at fields are nil, though I'm not clear on where they are null and where they are expected to be automatically set. Here is my User schema:

defmodule Blog.User do
  use Blog.Web, :model

  @required_fields [ :name, :encrypted_password, :email ]

  schema "users" do
    field :name, :string
    field :encrypted_password, :string
    field :email, :string

    timestamps
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, @required_fields)
  end
end

And the table, if it's relevant:

create table(:users) do
      add :name, :string
      add :encrypted_password, :string
      add :email, :string
      timestamps()
    end
    create unique_index(:users, [:email])

Am I doing it right? Thank you! 😄

trenpixster commented 8 years ago

Hey @dianjin! that's interesting, the example app has the same model and it's working correctly. What ecto version are you running?

dianjin commented 8 years ago

My deps:

defp deps do
    [{:phoenix, "~> 1.2"},
     {:phoenix_ecto, "~> 3.0"},
     {:absinthe_plug, "~> 1.1"},
     {:postgrex, ">= 0.0.0"},
     {:timex, ">= 0.0.0"},
     {:phoenix_html, "~> 2.3"},
     {:phoenix_live_reload, "~> 1.0", only: :dev},
     {:ecto, "~> 1.0.0", override: true},
     {:cowboy, "~> 1.0"},
     {:addict, "~> 0.3"}
   ]
  end

I added the "override:true" to ecto since when I first added the addict dependency, mix deps.get failed because of conflicting inter-dependencies or something of that nature - a post on StackOverflow suggested to add the "override: true" which then enabled me to run mix deps.get with addict.

trenpixster commented 8 years ago

Ohh I see. So, Addict now depends on Ecto 2.0. Do you have any hard dependency on Ecto 1.0? Please check #98 for more information on it.

An alternative would be to use version 0.2.5 of Addict, but you'll be missing out new features and enhancements that will appear in the future.

dianjin commented 8 years ago

Okay, I changed my deps to ecto version 2.0.0 and it worked. Thanks for the assistance! 😃

trenpixster commented 8 years ago

Glad it fixed it :smile: