I'm trying to associate two models (user & country) where:
· A country has_many users.
· A user belongs_to one country.
Also, I use UUIDs as Primary Key for the two models, so I don't have an :id type to relate them and only have strings to do it. The countries are static but I will create a lot of users.
How should I relate this two models and which parameters should I write in the two functions in the schemas (has_many & belongs_to)? And which function should I call to relate them: cast_assoc, build_assoc or put_assoc?
Thanks!!
@primary_key {:uuid, :string, []}
@derive {Phoenix.Param, key: :uuid}
schema "countries" do
field :country_name, :string
has_many :users, UserManagement.User, foreign_key: :country_id
end
@primary_key {:uuid, :string, []}
@derive {Phoenix.Param, key: :uuid}
schema "users" do
field :nickname, :string
field :password, :string, virtual: true
belongs_to :country, UserManagement.Country, references: :uuid
timestamps()
end