I know on_delete: :delete_all) will delete all related business_users if the role is deleted. But how to i protect this role from accidentally deleted?
Because i came from Django, it has on_delete=PROTECT to protect the role from deleted and error raised. I am new to ECTO, i not sure if there is anything I am missing here
above code just do data consistency, it is not helpful for user experience —— You shouldn’t just popup a model, and tell users “database error occurs”.
try to use Ecto.Changeset.no_assoc_constraint before deleting any referenced rows, :on_delete is just the last line of defense for data consistency.
thanks for the datailed explanation! one question for the :nothing, does it delete any data? example, if i delete a role, the associated business_user data and the role data remained and raise error?
It seems like PROTECT is a bit more strict than the SQL RESTRICT, but it is also purely application code, on_delete on django models is not a db constraint.
Unless you have a good reason or is handling it at the application level, it is nor recommended to use :nothing for references because it will make the data in the db inconsistent, pointing to role_ids that do not exist anymore, if you want to just remove all the references from the business_user you could use :nilify_all, it will not prevent the deletion of a role, but when the role is deleted all references pointing to it will be set to NULL.
Ecto is way more powerful than a simple database wrapper and can fetch and query data from basically any source, you can set up an Ecto.Repo that can fetch/update answers from a json api (or multiple data sources at once) and format/validate it using an Ecto.Schema and your app wouldn’t have any idea that it would be data from an json api and not an DB, because of this the Ecto documentation focus in the most generic abstracted parts, migrations are something pretty much SQL related and they are in their own ecto_sql module, but I agree that for someone that isn’t used to it it can be a bit confusing.
Hey, don’t forget that part of the Repo config options that you can supply in your config/**.exs files (only those that pertain to actual DB connections, and they are a lot) are actually documented inside DBConnection.start_link/2.