Unable to delete environments via the API

Problem:

Unable to remove Environments in Foreman version 6.3.1 via the API.
We have a script which uses the api to delete the environment once the respective branch from git has been deleted. However after upgrading foreman to version 6.3.1 this is throwing the error:

2023-11-13T15:37:03 [W|app|2b91c112] Action failed
2023-11-13T15:37:03 [I|app|2b91c112] Backtrace for 'Action failed' error (ActiveRecord::InvalidForeignKey): PG::ForeignKeyViolation: ERROR:  update or delete on table "environments" violates foreign key constraint "environment_classes_environme
nt_id_fk" on table "environment_classes"
 2b91c112 | DETAIL:  Key (id)=(2151) is still referenced from table "environment_classes".

The api call we use is:

deleted = api.resource(:environments).action(:destroy).call(:id => environment_id) # delete the environment

Our database has this in the database:

foreman=# select * from environments where id = 2150;
  id  |                                       name                                        |         created_at         |         updated_at
------+-----------------------------------------------------------------------------------+----------------------------+----------------------------
 2150 | our_branch_name | 2023-11-13 14:34:33.863251 | 2023-11-13 14:34:33.863251


foreman=# select * from environment_classes where environment_id = 2150 limit 5;
 puppetclass_id | environment_id |   id    | puppetclass_lookup_key_id
----------------+----------------+---------+---------------------------
              1 |           2150 | 8507288 |
              1 |           2150 | 8507289 |                      1792
              1 |           2150 | 8507290 |                      1793
              1 |           2150 | 8507291 |                      2142
              7 |           2150 | 8507229 |
(5 rows)


foreman-# \d+ environments
                                                                   Table "public.environments"
   Column   |            Type             | Collation | Nullable |                 Default                  | Storage  | Compression | Stats target | Description
------------+-----------------------------+-----------+----------+------------------------------------------+----------+-------------+--------------+-------------
 id         | integer                     |           | not null | nextval('environments_id_seq'::regclass) | plain    |             |              |
 name       | character varying(255)      |           | not null |                                          | extended |             |              |
 created_at | timestamp without time zone |           | not null |                                          | plain    |             |              |
 updated_at | timestamp without time zone |           | not null |                                          | plain    |             |              |
Indexes:
    "environments_pkey" PRIMARY KEY, btree (id)
Referenced by:
    TABLE "environment_classes" CONSTRAINT "environment_classes_environment_id_fk" FOREIGN KEY (environment_id) REFERENCES environments(id)
    TABLE "host_puppet_facets" CONSTRAINT "fk_rails_2640d30897" FOREIGN KEY (environment_id) REFERENCES environments(id)
    TABLE "hostgroup_puppet_facets" CONSTRAINT "fk_rails_d5e8be6d61" FOREIGN KEY (environment_id) REFERENCES environments(id)
    TABLE "template_combinations" CONSTRAINT "template_combinations_environment_id_fk" FOREIGN KEY (environment_id) REFERENCES environments(id)
Access method: heap

Removing the environment via the GUI still works fine.

Expected outcome:
To remove the Environment via the API

Foreman and Proxy versions:
Foreman 3.6.1
Foreman-proxy 3.6.1

Foreman DB:
Postgres 14

Foreman and Proxy plugin versions:
N/A

Distribution and version:
Ubuntu 20.04.6 LTS

Which foreman_puppet version are you using?

Hi, sorry should have included that,
We’re running: foreman_puppet 5.1.2