danschultzer / phoenix_oauth2_provider

Get an OAuth 2 provider running in your phoenix with controllers, views and models in just two minutes
MIT License
84 stars 41 forks source link

Error on application's delete #32

Open lucasbesen opened 4 years ago

lucasbesen commented 4 years ago

I'm having this error trying to delete a application

[error] #PID<0.759.0> running MyAppWeb.Endpoint (connection #PID<0.757.0>, stream id 2) terminated
Server: localhost:4000 (http)
Request: POST /oauth/applications/1472dc4ab4a92e7fbaa0c77e76331104d66148078d6fec281b1402adf4007ce1
** (exit) an exception was raised:
    ** (Ecto.ConstraintError) constraint error when attempting to delete struct:

    * oauth_access_grants_application_id_fkey (foreign_key_constraint)

If you would like to stop this constraint violation from raising an
exception and instead add it as an error to your changeset, please
call `foreign_key_constraint/3` on your changeset with the constraint
`:name` as an option.

The changeset has not defined any constraint.

        (ecto) lib/ecto/repo/schema.ex:689: anonymous fn/4 in Ecto.Repo.Schema.constraints_to_errors/3
        (elixir) lib/enum.ex:1336: Enum."-map/2-lists^map/1-0-"/2
        (ecto) lib/ecto/repo/schema.ex:674: Ecto.Repo.Schema.constraints_to_errors/3
        (ecto) lib/ecto/repo/schema.ex:451: anonymous fn/10 in Ecto.Repo.Schema.do_delete/4
        (phoenix_oauth2_provider) lib/phoenix_oauth2_provider/controllers/application_controller.ex:77: PhoenixOauth2Provider.ApplicationController.delete/4
        (phoenix_oauth2_provider) lib/phoenix_oauth2_provider/controllers/application_controller.ex:1: PhoenixOauth2Provider.ApplicationController.action/2
        (phoenix_oauth2_provider) lib/phoenix_oauth2_provider/controllers/application_controller.ex:1: PhoenixOauth2Provider.ApplicationController.phoenix_controller_pipeline/2
        (phoenix) lib/phoenix/router.ex:288: Phoenix.Router.__call__/2
        (myapp) lib/myapp_web/endpoint.ex:1: MyAppWeb.Endpoint.plug_builder_call/2
        (myapp) lib/plug/debugger.ex:122: MyAppWeb.Endpoint."call (overridable 3)"/2
        (myapp) lib/myapp_web/endpoint.ex:1: MyAppWeb.Endpoint.call/2
        (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:42: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy) /Users/lucasbesen/Documents/myapp/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
        (cowboy) /Users/lucasbesen/Documents/myapp/deps/cowboy/src/cowboy_stream_h.erl:320: :cowboy_stream_h.execute/3
        (cowboy) /Users/lucasbesen/Documents/myapp/deps/cowboy/src/cowboy_stream_h.erl:302: :cowboy_stream_h.request_process/3
        (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
danschultzer commented 4 years ago

You'll need to update the foreign key constraint so the associations will be deleted, like:

add :application_id, references(:oauth_applications, on_delete: :delete_all, type: binary_id)

By default ExOauth2Provider just adds on_delete: :nothing. I think it should be :delete_all or :nilify_all instead. I'll think about which makes more sense and create a PR.