In a mobile app I’m building, login and signup flow are the same. User submits phone number, receives a OTP (one time password) via SMS, inputs into the app, and gets logged in.
users
and one_time_passwords
are in separate tables
- If user exists (unique constraint on phone_number) new associated one_time_password record should be created
- If user does not exist, both should be created
I figure on the user
table, this is an upsert, so Repo.insert
call should have the on_conflict
param set (prob to :nothing
for now), and then, somehow, I should insert the one_time_password
. However the docs warn about upserts and associations:
Because of the inability to know if the struct is up to date or not, inserting a struct with associations and using the
:on_conflict
option at the same time is not recommended, as Ecto will be unable to actually track the proper status of the association.
I have 2 questions:
- How do I insert the
one_time_password
associated with itsuser
, in one go (one transaction?), afteruser
upsert? - Does the warning from the docs above relate to my issue or am I misunderstanding the intent?