Skip to content

Instantly share code, notes, and snippets.

@lytedev
Last active March 28, 2019 22:54
Show Gist options
  • Save lytedev/4fed404cf09af792be81932b6b825111 to your computer and use it in GitHub Desktop.
Save lytedev/4fed404cf09af792be81932b6b825111 to your computer and use it in GitHub Desktop.
Ecto Many to Many
def change do
# indices omitted
create table(:products) do
add(:name, :string)
end
create table(:product_attributes) do
add(:property, :json)
add(:product_type_id, :id)
end
create table(:products_product_attributes) do
add(:product_id)
add(:product_attribute_id)
end
create table(:product_types) do
add(:name, :string)
end
end
schema "products" do
field(:name, :string)
many_to_many(:product_attributes, MyApp.ProductAttribute,
join_through: MyApp.ProductsProductAttribute
)
end
schema "product_attributes" do
field(:property, :json)
has_one(MyApp.ProductType)
end
schema "product_type" do
field(:name, :string)
end
schema "product_product_attributes" do
belongs_to(:product, MyApp.Product)
belongs_to(:product_attribute, MyApp.ProductAttribute)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment