ActiveRecord::RecordInvalid: Validation failed: Subscription can't be blank, A Pool and its Subscription cannot belong to different organizations

Problem:
When running the command ‘foreman-rake katello:reimport --trace’ we get the following error:
** Execute katello:reimport
Importing Katello::Subscription
Importing Katello::Pool
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Subscription can’t be blank, A Pool and its Subscription cannot belong to different organizations

I had a look in the database, and all entries in the katello_pools table and in the katello_subscriptions table have organization_id = 1. So I have no idea where this error comes from.

Expected outcome:
successful re-import :slight_smile:

Foreman and Proxy versions:
3.3 + Katello 4.5

Distribution and version:
Centos 7 + PgSQL 11.

Other relevant data:

# foreman-rake katello:reimport --trace
** Invoke katello:reimport (first_time)
** Invoke dynflow:client (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute dynflow:client
** Invoke katello:check_ping (first_time)
** Invoke environment
** Invoke dynflow:client
** Execute katello:check_ping
** Execute katello:reimport
Importing Katello::Subscription
Importing Katello::Pool
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Subscription can't be blank, A Pool and its Subscription cannot belong to different organizations
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/validations.rb:80:in `raise_validation_error'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/validations.rb:53:in `save!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:318:in `block in save!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:375:in `block in with_transaction_returning_status'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `block in transaction'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/connection_adapters/abstract/transaction.rb:280:in `block in within_new_transaction'
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/opt/theforeman/tfm/root/usr/share/gems/gems/activesupport-6.0.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/connection_adapters/abstract/transaction.rb:278:in `within_new_transaction'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `transaction'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:212:in `transaction'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:366:in `with_transaction_returning_status'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/transactions.rb:318:in `save!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/suppressor.rb:48:in `save!'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.5.0/app/lib/katello/lazy_accessor.rb:67:in `save!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/persistence.rb:55:in `create!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:115:in `block in create!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:407:in `block in scoping'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:787:in `_scoping'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:407:in `scoping'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:115:in `create!'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation.rb:124:in `first_or_create!'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.5.0/app/models/katello/glue/candlepin/candlepin_object.rb:31:in `block in import_candlepin_record'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.5.0/app/lib/katello/util/support.rb:97:in `active_record_retry'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.5.0/app/models/katello/glue/candlepin/candlepin_object.rb:30:in `import_candlepin_record'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.5.0/app/models/katello/glue/candlepin/pool.rb:66:in `import_candlepin_record'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.5.0/app/models/katello/glue/candlepin/candlepin_object.rb:17:in `block in import_candlepin_records'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.5.0/app/models/katello/glue/candlepin/candlepin_object.rb:16:in `each'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.5.0/app/models/katello/glue/candlepin/candlepin_object.rb:16:in `import_candlepin_records'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.5.0/app/models/katello/glue/candlepin/candlepin_object.rb:53:in `block in import_all'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation/delegation.rb:88:in `each'
/opt/theforeman/tfm/root/usr/share/gems/gems/activerecord-6.0.4.7/lib/active_record/relation/delegation.rb:88:in `each'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.5.0/app/models/katello/glue/candlepin/candlepin_object.rb:51:in `import_all'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.5.0/lib/katello/tasks/reimport.rake:27:in `block (3 levels) in <top (required)>'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.5.0/lib/katello/tasks/reimport.rake:25:in `each'
/opt/theforeman/tfm/root/usr/share/gems/gems/katello-4.5.0/lib/katello/tasks/reimport.rake:25:in `block (2 levels) in <top (required)>'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:281:in `block in execute'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:281:in `each'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:281:in `execute'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:199:in `synchronize'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:199:in `invoke_with_call_chain'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/task.rb:188:in `invoke'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:160:in `invoke_task'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:116:in `each'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:116:in `block in top_level'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:125:in `run_with_threads'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:110:in `top_level'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:83:in `block in run'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:186:in `standard_exception_handling'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/lib/rake/application.rb:80:in `run'
/opt/rh/rh-ruby27/root/usr/share/gems/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/opt/rh/rh-ruby27/root/usr/bin/rake:23:in `load'
/opt/rh/rh-ruby27/root/usr/bin/rake:23:in `<main>'
Tasks: TOP => katello:reimport

This is being tracked in Bug #36025: Subscription can't be blank, A Pool and its Subscription cannot belong to different organizations - Katello - Foreman.