Error on upgrading from 0.47.4 to 0.50.23 : fk_data_permissions_ref_db_id

Upgrading from 0.47.4 to 0.50.23 my upgrade crashed with an error relating to a violation of the fk_data_permissions_ref_db_id constraint on the data_permissions table. I use Mariadb as the database for Metabase.

I corrected the error like this:

  1. I went into the mariadb console with my metabase database: sudo mariadb metabase
  2. I removed the constraint alter table data_permissions drop constraint fk_data_permissions_ref_db_id;
  3. I returned to the console and restarted metabase - which now ran without error.
  4. Back to mariadb, I removed records from the data_permissions table that were violating the constraint: delete from data_permissions where id in ( select p.id from data_permissions p where (select count(*) from metabase_database db where db.id=p.db_id)=0 );
  5. And recreated the constraint on the table: alter table data_permissions add constraint fk_data_permissions_ref_db_id foreign key (db_id) references metabase_database(id) on delete cascade;

Most likely, I only needed step 4.
Just posting this here because 1) I deleted some table rows to make the upgrade work, and perhaps someone would have some opinion on that and 2) someone else might find this error/report and solution useful.
Kind regards, C

How did the permissions structure end up?