The correct way to add additional columns into `users` table

I have used phx.gen.auth to create User schema and the Accounts context. The table is the standard: users.

I would like to add one additional column user_profile_id (integer) into the users table to link to the users_profile table with additional user info.

My question is what is the standard/official suggested way to do it. Can it be done via manually editing files only using the mix command? Perhaps making sure of taking care of the orphans in users_profile when deleting the user?

Thanks in advance.

Does the column have to live on users? It’s a lot easier to enforce constraints like “every profile must have a user” if it’s on profiles instead, and the dependency structure is better (users don’t know about profiles, but profiles know about users)

1 Like

Thanks. Why do you think it is better? I see more conflicts down the line using your suggestion.

Your suggestion makes sense for user has many profiles, which is not the case.

For example, if you delete a user how would the DB know which user_profile to delete if there is no reference to the users_profile table at all?

Don’t get me wrong, but I would strongly prefer the “pointer to the profile” solution.

The whole point of phx.gen.auth and other generators is that they provide a starting point. After running the command, you can and should edit the code to fit your needs. The drawback is that you’ll have to manually backport any modifications to the generators that you’d like to bring to the already generated code - re-running the generators most probably won’t work.

Regarding the design - it depends on your app, I guess. But you can delete profiles which will cascade to users, if that makes sense.

1 Like