Skip to content

Instantly share code, notes, and snippets.

@nietaki
Created October 15, 2019 21:49
Show Gist options
  • Save nietaki/ef60653ff5294f48ad6e188a2c407340 to your computer and use it in GitHub Desktop.
Save nietaki/ef60653ff5294f48ad6e188a2c407340 to your computer and use it in GitHub Desktop.
defmodule BlogExample.Schema.Category do
use Ecto.Schema
@primary_key {:name, :string, []}
embedded_schema do
field(:current_popularity, :integer)
end
end
defmodule BlogExample.Schema.Post do
use Ecto.Schema
schema "posts" do
field(:title, :string, null: false)
field(:contents, :string, null: false)
# this is new
belongs_to(:category, BlogExample.Schema.Category,
foreign_key: :category_name,
references: :name,
type: :string
)
belongs_to(:user, BlogExample.Schema.User)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment