DB error after migrating to v4 relating to violating foreign key constraints

I’ve upgraded from v3 to v4 successfully on production, but in the process of copying everything back over to my dev server, I’m running into an error that I can’t figure out how to resolve.

I’m running on Ubuntu 18.04, which only supports node 16 - although I know that’s below what Strapi requires, I don’t think its related to this issue because its working on my production server (same set up).

When I run npm run develop, it fails during the ‘Loading Strapi’ step, with this error:

error: alter table “public”.“pd_agencies” add constraint “pd_agencies_updated_by_id_fk” foreign key (“updated_by_id”) references “public”.“admin_users” (“id”) on delete SET NULL - insert or update on table “pd_agencies” violates foreign key constraint "pd_agencies_updated_by_id_fk

Attached is a screenshot with the full stacktrace.

I vaguely understand that the error is related to a problem with the foreign key constraints between 2 tables (pd_agencies and admin_users) but I don’t know really know how to go about solving it. Where do I look in my strapi file structure for where this code is coming from? As far as I know, the source files and database structure are the same between the two servers but obviously something is different, I just don’t know where to start digging since the stacktrace only points to node_modules and not my code.

Thanks for any pointers here!

This topic has been created from a Discord post (1220342175720800266) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord

Of course, as soon as I posted I made some progress on this. I can now tell that this isn’t really a Strapi issue at all but a data issue. Something I did in migrating the database broke the foreign keys. I don’t know how that happened because I dumped the whole table and imported the whole table - if anyone has specific recommendations for migrating postgres databases without breaking the foreign key links, I’d love to hear it because this is a pain to fix.