Skip to content
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

AttributeError: 'CachedReferenceField' object has no attribute '_get_db' #1075

Closed
yarneo opened this issue Jul 18, 2015 · 2 comments
Closed
Labels

Comments

@yarneo
Copy link

yarneo commented Jul 18, 2015

After appending an object to a list of CachedReferenceFields, I get this error when trying to fetch the object:

AttributeError: 'CachedReferenceField' object has no attribute '_get_db'

File "build/bdist.macosx-10.10-x86_64/egg/mongoengine/dereference.py", line 145, in _fetch_objects
references = doc_type._get_db()[collection].find({'_id': {'$in': refs}})
AttributeError: 'CachedReferenceField' object has no attribute '_get_db'

Any clue?

@nicktobolski
Copy link

nicktobolski commented May 23, 2016

We're having the same issue except with EmbeddedDocumentField instead of CachedReferenceField.

To elaborate, in line 130 of mongoengine/dereference.py - where it checks hasattr(collection,'objects') - it returns false where it should be true.

We ran a debugger and checked hasattr(collection,'objects'). This returned False first, and True immediately after even though it's exactly (or should be) the same object being checked.

We were able to temporarily fix this issue but adding line 130 as pictured below to the above referenced dereference.py:
pasted_image_at_2016_05_23_06_48_pm

@bagerard
Copy link
Collaborator

bagerard commented Sep 4, 2018

I believe the root cause of your problem is hidden by hasattr. In fact under python2 hasattr swallows any errors that occurs within the call and returns False (as if the attribute simply does not exist, hiding the exception that occured). hasattr(collection, 'objects') is actually going through some code (including potentially creating indexes if they don't exist yet).

The behavior you describe is similar to #1688 and #1522.
That part of the code got improved (#1858) by replacing the hasattr by getattr which should allow you to get the actual exception. Retry your code using the latest master, you should get the actual exception now.

@bagerard bagerard closed this as completed Nov 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants