![]() Wrapping UpĪs Ecto has matured in the past few years, it has evolved into more than just a simple database library. This feature makes it possible for us to enforce that the correct org_id value is set for all resources at Slab, enabling us to securely and reliably support multi-tenancy in our product. ![]() We extensively use this feature at Slab, where all tables in our database, including pivot tables, have an org_id foreign key that references the team the data belongs to. This is how you would define the schema:ĭefmodule MusicApp. We also assume that the same tracks can appear on multiple albums, and like to ensure that the label_id is consistently and automatically set on the join tables every-time they are added. when readying your database to support sharding aka vertical partitioning). This is a common scenario in applications where one schema (like a Team or an Organization) "owns" all other data, including associations and joins for security, privacy or performance reasons (e.g. Here's where :join_defaults comes in.īuilding on the previous Music App example again, we introduce a RecordLabel schema that acts as an umbrella for all child data by having every other schema reference it directly. Out of the box, Ecto handles the former case very gracefully by using the :join_through option in a many_to_many relation in an Ecto schema, but it can get messy for the latter. While in the majority of cases a join table will only have two foreign keys (IDs of the records from the two tables with the association), it's not unusual to have other columns or foreign keys to track more information. Starting in v3.3.2, Ecto added support for a new option :join_defaults for many-to-many associations which behaves similar to the :defaults option but instead of setting attributes on the child association, it sets them on the intermediate association for the join table. Using this you can also offload other association-related runtime logic directly to Ecto that will be executed whenever a child association is added, instead of doing it manually.Īt Slab, this allows us to ensure all comments belong to the correct post, even though the parent is a comment thread (where a Post can have many threads). This will make sure that anytime a new track association is added to an album record, it automatically sets the track's artist to the album's if another artist is not specified. Defining Defaults at Run-timeįortunately, Ecto supports passing an MFA tuple (Module, Function, Arguments - used in the form of end end Normally you would extend the changeset method of the schema to handle these situations but the drawback there is that you can't look at the parent record's attributes to set values on the child association. Similarly, Single Table Inheritance with multiple parent tables associating with the same table is a valid use-case but these are rare scenarios and mostly limited by the value defined at compile-time. Has_many :instrumentals, Track, where:, defaults: Has_many :vocals, Track, where:, defaults:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |