Last active
March 28, 2019 22:54
-
-
Save lytedev/4fed404cf09af792be81932b6b825111 to your computer and use it in GitHub Desktop.
Ecto Many to Many
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
schema "products" do | |
field(:name, :string) | |
many_to_many(:product_attributes, MyApp.ProductAttribute, | |
join_through: MyApp.ProductsProductAttribute | |
) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
schema "product_attributes" do | |
field(:property, :json) | |
has_one(MyApp.ProductType) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
schema "product_type" do | |
field(:name, :string) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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