Foreign constraint in initial database

Hi. When exploring the Nextcloud database, I can see it’s a flat database. There’s no constraint between tables. I wonder about data integrity. For example, what would happen with the related records in oc_accounts_data if I deleted a record in oc_accounts? With a foreign key on cascade delete, the related records would be deleted. But there’s no constraint here.

As you have correctly identified, most probably - nothing. The tables are just tables. It might be probably to add constraints as we discussed earlier but this is not really a hard requirement.

In fact, there are a few things to consider and some rules to obey here.

  1. Every app only uses its own tables or the API. No app should use other tables lightly. The layout might change, the table might have foreign keys that get broken etc.
  2. Put all access in a transaction of you need to do something atomically. That way, the database prevents race conditions internally.
  3. The database should be used by Nextcloud only. No other apps or whatever should work directly on the database. Instead provide rest API to carry out the updates.

That way, your app serves as a gate keeper to the database and kann guarantee a safe and sane state. There are very few everyone to these rules and you should really be careful and mindful of your start bending and breaking them.