octabytes / FireO

Google Cloud Firestore modern and simplest convenient ORM package in Python. FireO is specifically designed for the Google's Firestore
https://fireo.octabyte.io
Apache License 2.0
249 stars 29 forks source link

On multiple requests: `TypeError: sequence item 1: expected str instance, MetaField found ` #108

Open micheledicosmo opened 3 years ago

micheledicosmo commented 3 years ago

The first time I run the following code, it runs ok. The second time I run it without restarting the program, I get this error.

This happens both in debug mode and in production.

class Log(Model):
    id = fields.IDField
    time = fields.DateTime()
    component = fields.TextField()
    message = fields.TextField()
    payload = fields.TextField()
    payload_json = fields.MapField()
    level = fields.TextField()

    class Meta:
        collection_name = "logs"
logger = logging.getLogger(__name__)

def debug(component:str, message:str, payload=None):
    payload_json = None
    if not isinstance(payload, dict):
        try:
            payload_json = json.loads(payload)
        except:
            logger.exception(f'Exception while deserializing a JSON payload: {payload}')

    models.Log(
        time=datetime.datetime.now(),
        level='debug',
        component=str(component),
        message=str(message),
        payload=str(payload),
        payload_json=payload_json,
    ).save()
Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/workspace/blueprints/....py", line 155, in ...redacted...
    components.dblog.debug(...redacted...)
  File "/workspace/components/dblog.py", line 20, in debug
    models.Log(
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/fireo/models/model.py", line 346, in save
    return self.__class__.collection.create(self, transaction, batch, merge, **self._get_fields())
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/fireo/managers/managers.py", line 230, in create
    return self.queryset.create(mutable_instance, transaction, batch, merge, **field_list)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/fireo/queries/query_set.py", line 53, in create
    return CreateQuery(self.model_cls, mutable_instance, **kwargs).exec(transaction_or_batch, merge)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/fireo/queries/create_query.py", line 151, in exec
    return query_wrapper.ModelWrapper.from_query_result(self.model, self._raw_exec(merge=merge))
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/fireo/queries/create_query.py", line 132, in _raw_exec
    ref = self._doc_ref()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/fireo/queries/create_query.py", line 73, in _doc_ref
    return self.get_ref().document(self.model._id)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/collection.py", line 109, in document
    return self._client.document(*child_path)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/client.py", line 317, in document
    joined_path = _helpers.DOCUMENT_PATH_DELIMITER.join(path)
TypeError: sequence item 1: expected str instance, MetaField found 
AxeemHaider commented 3 years ago

It should have to save. It seems this problem is from Firestore. Try to save it using firestore native lib. And other possible problem is in your data

IshuSingh1 commented 3 years ago

I am also currently stuck due to this error. Were you able to find any fixes OP?

micheledicosmo commented 3 years ago

It happens in what appears to seem randomly and in some code paths it always happens. Unfortunately I didn’t isolate the issue nor found a solution.

On Sun, 27 Jun 2021 at 06:26, IshuSingh1 @.***> wrote:

I am also currently stuck due to this error. Were you able to find any fixes OP?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/octabytes/FireO/issues/108#issuecomment-869098540, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFLFZISYFYG7J7A5HB5USDTU2SARANCNFSM43GIOEZQ .

-- Sent from mobile

AxeemHaider commented 3 years ago

Please show off your data which you are trying to save.

Below example is working fine for me. Even I try it multiple time.

from fireo import fields
from fireo.models import Model
from datetime import datetime

class Log(Model):
    id = fields.IDField
    time = fields.DateTime()
    component = fields.TextField()
    message = fields.TextField()
    payload = fields.TextField()
    payload_json = fields.MapField()
    level = fields.TextField()

    class Meta:
        collection_name = "logs"

payload_json = {'name': 'json_payload'}

log = Log(
        time=datetime.now(),
        level='debug',
        component=str('component'),
        message=str('message'),
        payload=str('payload'),
        payload_json=payload_json,
    ).save()

print(log.key)
IshuSingh1 commented 3 years ago

This is the method I created to add a document

COLLECTION = 'message_ids' def add_document(obj, username): data = {COLLECTION: get_message_id(obj), 'username': username, 'text': get_message( obj), 'sender_id': get_sender_id(obj), 'recipient_id': get_recipient_id(obj)} db.collection(COLLECTION).document(id).set(data)

FYI I am using native firebase_admin. But the error I am getting is the same error mentioned by OP. Please have a look and let me know if you see any issues with it. Thank you

AxeemHaider commented 3 years ago

it seems problem is in data please try to print out data and see if there is any problem or is it support type of Firestore if you are trying to save it throw native firebase_admin then probably there is problem in data or data type is not support by firestore.

Please try to print data. before saving

print(data)