No unique constraint matching given keys for referenced table "{TABLE}"

Odoo Error Fix

Sometimes when moving a database from one environment to another you may experience issues.  I have recently started to notice this particular error that will prevent you from installing modules, upgrading modules or the database.

Fortunately, there is a simple solution.

1. Log into your database (I use docker with compose, so that would look like this):
$ docker compose exec -it {service name} /bin/bash
$ psql -U {database_user} -d {database_name} 


2. Connect to the database that is exhibiting the issue
$ \c {database name} 


3. Add the constraint for the problem table:
ALTER TABLE {table} ADD CONSTRAINT unique_{table}_id UNIQUE (id); 

For example:

ALTER TABLE res_company ADD CONSTRAINT unique_res_company_id UNIQUE (id); 


I hope this helps!