1

Following is the error I get when I add data in the associated model from the master database.

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "configurations" violates foreign key constraint "fk_rails_29e23e6ceb" DETAIL: Key (company_id)=(129) is not present in table "companies".

Md. Farhan Memon
  • 6,055
  • 2
  • 11
  • 36
Qumber ali
  • 23
  • 9
  • share you model schemas.. – Md. Farhan Memon Jul 17 '17 at 07:09
  • Company has_many :configurations Configuration belongs_to :company ``` Company model is excluded from tenant, And Configuration model are in tenent``` – Qumber ali Jul 17 '17 at 07:16
  • `schema` as in table columns and datatypes and not `associations`.. – Md. Farhan Memon Jul 17 '17 at 07:19
  • ** create_table "configurations", force: :cascade do |t| t.string "key" t.string "value" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "catelog_source_id" t.boolean "visible", default: true t.integer "company_id" t.index ["catelog_source_id"], name: "index_configurations_on_catelog_source_id", using: :btree t.index ["company_id"], name: "index_configurations_on_company_id", using: :btree end – Qumber ali Jul 17 '17 at 07:22
  • [This answer](https://stackoverflow.com/a/36141248/59991) seems to indicate it isn't possible. – Cimm Apr 16 '20 at 13:58

1 Answers1

0

I had the same issue and spent a lot of time trying to figure what caused this. The solution is quite simple (at least in my case). Straight from the documentation of the gem:

If you have some models that should always access the 'public' tenant, you can specify this by configuring Apartment using Apartment.configure. This will yield a config object for you. You can> set excluded models like so:

config.excluded_models = ["User", "Company"]        # these models
 will not be multi-tenanted, but remain in the global (public)
 namespace  ```

So just add it your apartment.rb file.

Apartment.configure do |config|
  config.excluded_models = %w{ User Company }
  ...
end
Kevin Kloet
  • 1,086
  • 1
  • 11
  • 21
alexts
  • 180
  • 2
  • 13