tfwright / live_admin

Low-config admin UI for Phoenix apps, built on LiveView
MIT License
251 stars 22 forks source link

Edit and view Associations #15

Closed AlexParamonov closed 1 year ago

AlexParamonov commented 1 year ago

Hello,

First of all, you did a great job with embedded schemas: it works out of the box and saves perfectly, even for custom types! Impressive, other admin solutions failed to do so!

Unfortunately, all other schema-to-schema or table-to-table associations (1 to many, many to many) can only display a disabled id field. Is there a way to render such associations similar to embedded ones? Could you please let me know where help is needed and what is already in progress?

Best, Alexander

tfwright commented 1 year ago

Hi Alexander,

Currently (v 0.5.1) there is support for editing belongs_to associations. So, it is also possible to "edit" has_one and has_many through the schema that has the fk, although admittedly it's not extremely convenient currently. The only edit it's not possible to make with the built in components are many_to_many. I'm certainly open to adding support for these, but there are various UX questions it raises. How would you envision it working?

AlexParamonov commented 1 year ago

Should it work with with binary_id as well?

I have the schema:

defmodule Schema.ConversationMessage do
  use Schema.UUID
  import Ecto.Changeset

  schema "conversation_messages" do
    belongs_to :conversation, Schema.Conversation
    belongs_to :participant, Schema.User

    field :content, :string

    timestamps()
  end

  @doc false
  def validate(message) do
    message
    |> validate_required([:content])
  end
end

defmodule Schema.UUID do
  defmacro __using__(_) do
    quote do
      use Ecto.Schema
      @primary_key {:id, :binary_id, autogenerate: true}
      @foreign_key_type :binary_id
    end
  end
end

Keys in db:

table_schema,constraint_name,table_name,column_name,foreign_table_schema,foreign_table_name,foreign_column_name
public,conversation_messages_conversation_id_fkey,conversation_messages,conversation_id,public,conversations,id
public,conversation_messages_participant_id_fkey,conversation_messages,participant_id,public,users,id

Live Admin UI: image

tfwright commented 1 year ago

Hm, I don't think editing binary ids are currently fully supported. Adding support might be as easy as updating this guard clause but I would need to look into that a bit more.

tfwright commented 1 year ago

@AlexParamonov this seemed pretty straightforward to add so I went ahead and implemented it in https://github.com/tfwright/live_admin/commit/27dee2e8143e034814630fb943dbd006ad74432f

When you have a chance, can you try out that ref in your app, and let me know how it works?

AlexParamonov commented 1 year ago

@tfwright It works like a charm!

AlexParamonov commented 1 year ago

Thanks a lot for the swift update!