chopralab / cipher_db

Repository for RESTful API, GUI, and Mongo DB triggers for the cipher database
2 stars 0 forks source link

[BUG] `Compounds` document is not properly structured #9

Closed davidegraff closed 2 years ago

davidegraff commented 2 years ago

Summary

Trying to iterate through the compounds collection in our MongoDB using the Compounds document fails

Background

using mongoengine, you can iterate through documents in your database like so:

for user in User.objects:
    print user.name

The above code block would iterate through the user collection in your MongoDB, seamlessly parsing all of the documents into the corresponding User document in the client codebase.

Expected behavior

>>> from cipher_identifiers.docs.docs import Compounds

>>> print(Compounds.objects)
[<Compounds: Compounds object>, ...]

Observed behavior:

>>> from cipher_identifiers.docs.docs import Compounds

>>> print(Compounds.objects)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/miniconda3/envs/cipher/lib/python3.8/site-packages/mongoengine/queryset/queryset.py", line 73, in __repr__
    self._populate_cache()
  File "/opt/miniconda3/envs/cipher/lib/python3.8/site-packages/mongoengine/queryset/queryset.py", line 129, in _populate_cache
    self._result_cache.append(next(self))
  File "/opt/miniconda3/envs/cipher/lib/python3.8/site-packages/mongoengine/queryset/base.py", line 1599, in __next__
    doc = self._document._from_son(
  File "/opt/miniconda3/envs/cipher/lib/python3.8/site-packages/mongoengine/base/document.py", line 836, in _from_son
    obj = cls(__auto_convert=False, _created=created, **data)
  File "/opt/miniconda3/envs/cipher/lib/python3.8/site-packages/mongoengine/base/document.py", line 99, in __init__
    raise FieldDoesNotExist(msg)
mongoengine.errors.FieldDoesNotExist: The fields "{'synonyms'}" do not exist on the document "Compounds"

Cause

The offending source is here. Our Compounds document has no synonyms field, but the documents in our backend DB do. We must either delete this field from each document, or update the Compounds document accordingly.

mmuhoberac commented 2 years ago

This is because I was working on a feature for compound searching and there is a mismatch between the structure and database, Sorry about not notifying. You can replace the current compounds object with the following:

class Compounds(me.Document):
    inchikey = me.StringField(required=True, primary_key=True)
    name = me.StringField(default="")
    smiles = me.StringField(required=True, validation=validate_smiles)
    inchi = me.StringField(required=True)
    cid = me.StringField(default="")
    iupac = me.StringField(default="")
    synonyms = me.ListField(me.StringField(), default = [])
    modified = me.DateTimeField(default=datetime.datetime.utcnow)
davidegraff commented 2 years ago

should probably delete the default=[] in the PR that addresses this because default arguments as mutable objects can lead to problems. The default is also already an empty list

mmuhoberac commented 2 years ago

I deleted it from my end