Disclaimer: Really new to Phoenix Elixir, just starting to learn/love it
I’m trying to create an Accounts table, where each account has a has_many relationship to an Attributes table, where the Attributes table holds a list of attribute name - value pairs
I want each account to be able to have a list of attributes, but the attribute table to not have to point back into the Accounts table.
Here’s my schema for the account table
defmodule AccountMaker.Account do
use Ecto.Schema
import Ecto.Changeset
alias AccountMaker.{Account, Attribute}
schema "accounts" do
field :auth_id, :string
field :password, :string
field :username, :string
has_many :attributes, Attribute
timestamps()
end
@doc false
def changeset(%Account{} = account, attrs) do
account
|> cast(attrs, [:auth_id, :username, :password])
|> validate_required([:auth_id, :username, :password])
end
end
However, I’m not quite sure how to write the migration for creating the account table, and was hoping to get some help with that.
Here’s my current migration
defmodule AccountMaker.Repo.Migrations.CreateAccounts do
use Ecto.Migration
def change do
create table(:accounts) do
add :auth_id, :string
add :username, :string
add :password, :string
# Need something for many attributes
timestamps()
end
end
end
Also, not sure if this helps clarify my question, but the Attributes schema looks like this, which is a tree hierarchy
defmodule AccountMaker.Attribute do
use Ecto.Schema
import Ecto.Changeset
schema "attributes" do
field :name, :string
field :value, :string
belongs_to :parent, AccountMaker.Attribute
end
end