Elixir ecto join11/24/2023 ![]() In this guide, it details an example of a simple Todo List. When I say nested forms, I'm referring to this guide titled Polymorphic associations with many to many. This is not to knock what the Phoenix Framework has provided to make things more convenient for us via nested forms but to demonstrate when it should be used. It also forced me to look for another way (see below) and also enabled me to (happily) restore the NOT NULL constraint. So, it showed me clearly that cast_assoc was not needed (and should not be used) at all if I were breaking down the operation into multiple INSERTs. What ended up happening, however, was two records for an organisation and a membership were being created – a record each for cast_assoc and a record each for the INSERT in the Multi Transaction. ![]() I first tried relaxing the constraint by removing the NOT NULL from the user_role field in the database and used the same Multi Transaction above, I was not a fan of doing this because it risks data integrity. And from what I've learned, cannot be set when using cast_assoc. However, because user_role has a NOT NULL constraint, an error was thrown because user_role was not set. Join_through: Īs mentioned, the error was thrown by the database and that's because cast_assoc automatically creates the INSERT for the JOIN TABLE. The culprit was Line 4, the cast_assoc, but digging deeper, it's because of the many-to-many association between User and Organisation that was defined by this: many_to_many :users, , When I submitted the form, the database would throw the following error.ĮRROR 23502 (not_null_violation) null value in column "user_role" violates not-null constraint Originally, the User.registration_changeset was this way because I was trying to make use of Phoenix's nested form handlers: 1 def registration_changeset(user, attrs, opts \\ ) doģ |> cast(attrs, )Ĥ |> cast_assoc(:organisations, required: false ) cast_assoc failed because of NOT NULL CONSTRAINT on JOIN TABLE field Some key notes for those who might need them. To illustrate, the struct returned by the form looks like this:.This is something I want the system to manage behind the scenes. I also do not want to expose the user_role field on the form.This part is key because it means that the User form has nested inputs for Organisation.When a new user signs up, the sign up form also provides a name field so the user can provide the name of their organisation.Membership records also have a user_role column to specify the role of the user in relation to the associated organisation (e.g., creator, administrator, etc.).To spell it out, Users have many Organisations via Memberships, and Organisations have many Users via Memberships.I have three schemas that form the many_to_many association: User, Organisation, and Membership.However, I’m finding it a bit difficult in my case of using a many_to_many association. I’m trying to make the most out of the conveniences/automagic of the Phoenix/Ecto frameworks to handle database INSERTs wherever possible.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |