dwyl / alog

🌲 alog (Append-only Log) is an easy way to start using the Lambda/Kappa architecture in your Elixir/Phoenix Apps while still using PostgreSQL (with Ecto).
GNU General Public License v2.0
15 stars 2 forks source link

Creating users with duplicate email is not erroring #57

Open RobStallion opened 5 years ago

RobStallion commented 5 years ago

I am currently using alog in another project where two tables are being updated at the same time.

The venue table has a many to many association with the users table. When a user of the site creates a new venue they can also create a new user at the same time.

However, for some reason the user changeset function is not working as expected here. It is allowing multiple users with the same email address to be created.

This does not happen when you try to create a user on their own (e.g. Call the User.insert function directly with a user changeset)

RobStallion commented 5 years ago

It appears that the reason for the error is because the user changeset function does not return an error when there is a duplicate email hash. That step looks like it is being done in alog.

Alog must have a function for checking unique constraints, but this step must be missed when the table is an association.

RobStallion commented 5 years ago

As there cannot be a unique index in alog (as many rows can relate to one user for example) it looks like alog is doing a version of this logic here.

I don't fully understand this function at the moment but it appears that it gets a list of constraints from changeset and reduces them, returning a changeset with errors added as needed. However it does not appear to work for associations.

I will take a deeper look at this but from what I have seen so far it appears that associations in changesets, are just a list of changesets themselves (at least in the case of a many to many association). This should mean that this function can be called recursively on the nested list of changeset.

I am not sure how the errors in nested changeset will effect the top level changeset at the moment.