use belongs_to instead of field for references

This commit is contained in:
2021-09-12 18:54:53 -04:00
committed by oliviasculley
parent f96956cf5e
commit 8827858204
4 changed files with 25 additions and 10 deletions

View File

@ -1,6 +1,7 @@
defmodule Cannery.Containers.Container do
use Ecto.Schema
import Ecto.Changeset
alias Cannery.{Accounts}
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
@ -9,7 +10,8 @@ defmodule Cannery.Containers.Container do
field :location, :string
field :name, :string
field :type, :string
field :user_id, :binary_id
belongs_to :user, Accounts.User
timestamps()
end
@ -17,7 +19,7 @@ defmodule Cannery.Containers.Container do
@doc false
def changeset(container, attrs) do
container
|> cast(attrs, [:name, :desc, :type, :location])
|> validate_required([:name, :desc, :type, :location])
|> cast(attrs, [:name, :desc, :type, :location, :user_id])
|> validate_required([:name, :desc, :type, :location, :user_id])
end
end