The use of __ in relationships and table naming?

So I think I learned something today from chatGPT that I was unaware of, but wanted to ask about it as chatGPT tends to just…make stuff up when it doesn’t know about things.

I had a form with multiple checkboxes, and every time the checkbox was clicked it was triggering a validation loop.

The original form was this:

  @impl true
  def handle_event("validate", %{"user_setting" => user_setting_params}, socket) do
    changeset =
      socket.assigns.user_setting
      |> User_Settings.change_user_setting(user_setting_params)
      |> Map.put(:action, :validate)
    {:noreply, assign(socket, :changeset, changeset)}
  end

ChatGPT gave me this:

  @impl true
  def handle_event("validate", %{"user__setting" => user_setting_params}, socket) do
    changeset =
      socket.assigns.user_setting
      |> User_Settings.change_user_setting(user_setting_params)
      |> Map.put(:action, :validate)
    {:noreply, assign(socket, :changeset, changeset)}
  end

And it worked.

The explanation I was give was this:

The difference between user__setting and user_setting is in the way they represent the relationship between the User and Setting models.

user_setting represents a one-to-one relationship between a User and a Setting. In this relationship, each user can have only one setting, and each setting belongs to only one user. This is typically used when the relationship between two models is exclusive and there can be only one instance of the related model for each instance of the main model.

On the other hand, user__setting represents a many-to-one relationship between a User and Setting. In this relationship, multiple settings can belong to the same user. This is typically used when the relationship between two models is non-exclusive and there can be multiple instances of the related model for each instance of the main model.

Does the above sound correct? It’s pretty difficult to find information on the significance of a double __ in a google search or looking through hexdocs as the double underscore appears all over the place

I basically want to know as a standalone double character, whats the purpose of __ in general?

Also when should I be using __ in my schema names? I have 2 schemas that automatically generated as a double space and they are both prefixed with “user”. Does ecto automatically make an association of sorts between existing tables if you use an underscore to name them?

For example if I have a “posts” schema and made a new schema called “post_info” would ecto make an association fo sorts between the two that I’m unaware of because the second schema starts with “post”?

Any info appreciated.

Ha, yeah this is entirely made up by Chat GPT. There is no such convention in Elixir / Phoenix.

12 Likes

Wonderful lol

Yeah this feels like a convention from some other framework the model has conflated to all snakecased relationship ORM code

+1 for “this is a hallucination, there’s no such thing in Phoenix”

The usage of double-underscores reminds me of Django’s cross-relationship query syntax:

Blog.objects.filter(entry__authors__name='Lennon')

but even there the single underscore form would usually mean literally “a relationship named user_setting:thinking: