![]() ![]() And always take time to learn, evolve and experiment new ways of doing things so that you don’t get stuck with outdated techniques. When a migration is run, Ecto checks the current version of the database schema and applies the necessary changes to bring the schema up to date. In order to execute our migration, we must navigate to the root of the project and run the following command mix ecto.migrate. In short, the lesson to take from here is to: not always go for the easiest solution as it might not be the best in the long term. To successfully run the migration, we can simply call Mix tasks. defmodule """īelongs_to(:user, User, primary_key: true)īelongs_to(:project, Project, primary_key: ~w(user_id project_id)aĭef changeset(user_project, params \\ % User.changeset_update_projects(projects) defmodule doĬreate table(:user_project, primary_key: false) doĪdd(:project_id, references(:project, on_delete: :delete_all), primary_key: true)Īdd(:user_id, references(:user, on_delete: :delete_all), primary_key: true)Ĭreate(index(:user_project, ))Ĭreate module UserProject with schema and changeset. It worked perfectly fine, the unit tests proved the solution worked just fine!Ĭreate a migration that adds user_project table. In terms of database diagram/structure, I created both user and project tables, plus the so-called Many to Many table (user_project), as the diagram below illustrates.Īt first, I thought the easiest way to solve the problem was to scaffold each table and manually create a method that directly managed the projects from a user in the user_project table. You might ask yourself, what does this have to do with Elixir and Ecto? Well, at Coletiv we have been working on a project where a user can manage multiple projects and a project can be managed by multiple users, this is a trivial case known as many to many. But it doesn’t mean we are solving the problem right away. ![]() Meaning that most of the times, when we have a problem we take the easiest way to solve it. A University of College London study discovered something we all knew: humans are lazy. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |