-
Notifications
You must be signed in to change notification settings - Fork 801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalid single-table inheritance type: SubClass is not a subclass of Class #848
Comments
Hi @PhilCoggins Thanks for raising this issue. Would you mind providing an example so we can reproduce it? |
Thanks @estolfo, here is a sample app with instructions: https://github.com/PhilCoggins/elasticsearch-sti-fail LMK if you have any issues running. I tried to do this in a single script file but |
I'm also hitting this, will use the initializer patches for now. |
Looking at the code, I wonder if it makes sense to use the |
@darkhelmet You could, though that's already occurring in the Also want to point out that the initializer is wrapped in an environment check since I only need this on dev, not sure how it would behave in prod. |
Yeah, this is what I ended up with in my initializer. if Rails.env.development?
Rails.application.reloader.before_class_unload do
Elasticsearch::Model::Registry.remove_instance_variable(:@instance)
Elasticsearch::Model::Adapter::Multiple::Records.remove_class_variable(:@@__types)
end
end |
Is there any movement on a gem fix for this? I just ran into this and its a major issue. It only happens for me in Rails5, though which we are currently upgrading to. I've never encountered this in Rails4. |
@surjay I will look into it and have an update soon! |
@PhilCoggins I'm having trouble reproducing this using the example app you provided. I followed the steps in the
|
@estolfo thanks for reaching back out. I have updated the instructions in the repo. Also make sure you run the setup instructions at the top ( |
@PhilCoggins I've opened this pull request but I get a |
I would consider the Somehow, when a When code is reloaded, however, the Using your branch of
I hope this helps. |
@PhilCoggins thanks for the extra info, I'll keep investigating. |
I want to give an update on this: I've been looking into the cause of the inherited class not being autoloaded after That means we'll have to figure out another mechanism for supporting STI with Elasticsearch 7, as we can't rely on the automatic population of I'll have an update soon, thanks again for your patience. |
@PhilCoggins I think one of the ways to support STI going forward with the removal of types in Elasticsearch would be to add an artificial type field to each document if inheritance is detected. I did this in the past with Mongoid. What do you think about that approach? For example: class Animal < ApplicationRecord
include Elasticasearch::Model
end class Dog < Animal
end The name, d = Dog.create(name: 'Fluffy')
# => { name: 'Fluffy', _inheritance_type: 'dog' } Mongoid does something similiar in automatically adding a One drawback of this approach is that an extra field is added to the document and that field has to be in the mapping and added to queries. There are other ways to implement this but they are more manual and require user intervention. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Update: still seeing this in 7.2.1 Rails 5.2. After reloading the Elasticsearch::Model::Registry.all[0].descendants is blank, so the ActiveRecord::SubclassNotFound is being thrown. What is interesting is that I am putting my STI subclasses in the same index so should it not get instantiated using the base class? Following the If all Dogs are indexed in the @stalebot, please reopen - closing active issues does not make them go away. |
I encountered some serious headaches while working with
Elasticsearch-Model
in development.My problem is that the
Invalid single-table inheritance type: X is not a subclass of Y
exception was being thrown when I tried to instantiate records with theMultiple
adapter after my code had been reloaded for the first time. I would have to restart my Rails server every time I made any changes to my code.What I found was that
Elasticsearch::Model::Registry.models
would endlessly accumulate reloaded classes each time my code was being reloaded. If I had five classes that includedElasticsearch::Model
, it would increase by 5 each time my code reloaded. I have monkey-patched theRegistry
class with:in an initializer and only for development environment to fix this particular issue to ensure this Registry only contains the newly-reloaded classes.
After this change, I continued to encounter the same error. What I found was that
Elasticsearch::Model::Adapter::Multiple::Records
was caching the result of__type_for_hit
in a class variable@@__types
, and after my code was reloaded, the stale classes were being returned instead of the newly reloaded classes. I unset this class variable by hooking into Active Support's reloader:This is in the same environment block that the above monkey-patch is in to avoid affecting production environment. This allowed my records to be instantiated even after my code had been reloaded and fixed my issue.
This issue took a ton of time and effort (and frustration) to debug and fix, it would be nice to see the team provide better support for STI since this is a core feature in Rails. Hopefully this report will help you guys improve on some of these weak spots. I'm happy to provide any other information you might need.
The text was updated successfully, but these errors were encountered: