googleapis / python-firestore

Apache License 2.0
214 stars 73 forks source link

firestore.Increment returns error: "TypeError: Not a cmessage" #778

Closed akashrajkn closed 5 months ago

akashrajkn commented 11 months ago

Environment details

Steps to reproduce

Run below code.

Relevant package versions

google-api-core==2.11.0
google-api-python-client==2.86.0
google-auth==2.17.3
google-auth-httplib2==0.1.0
google-cloud-core==2.3.2
google-cloud-firestore==2.11.0
google-cloud-storage==2.8.0
google-crc32c==1.5.0
google-resumable-media==2.4.1
googleapis-common-protos==1.59.0
proto-plus==1.22.2
protobuf==4.22.3
grpcio==1.58.0
grpcio-status==1.58.0

Code example


db = firestore.client()
doc_ref = db.collection(<collection_id>).document(<doc_id>)
doc_ref.update({'total_duration': firestore.Increment(duration)})

Stack trace

Traceback (most recent call last):
  File "/home/ubuntu/my_server/my_server/api/utils/firebase.py", line 238, in create_or_update_voice_profile_document
    doc_ref.update({'total_duration': firestore.Increment(duration)})
  File "/home/ubuntu/miniconda3/envs/server_env/lib/python3.10/site-packages/google/cloud/firestore_v1/document.py", line 324, in update
    batch, kwargs = self._prep_update(field_updates, option, retry, timeout)
  File "/home/ubuntu/miniconda3/envs/server_env/lib/python3.10/site-packages/google/cloud/firestore_v1/base_document.py", line 239, in _prep_update
    batch.update(self, field_updates, option=option)
  File "/home/ubuntu/miniconda3/envs/server_env/lib/python3.10/site-packages/google/cloud/firestore_v1/base_batch.py", line 141, in update
    write_pbs = _helpers.pbs_for_update(
  File "/home/ubuntu/miniconda3/envs/server_env/lib/python3.10/site-packages/google/cloud/firestore_v1/_helpers.py", line 941, in pbs_for_update
    update_pb.update_transforms.extend(field_transform_pbs)
TypeError: Not a cmessage
hansheng654 commented 9 months ago

exact same problem, nothing on internet helps. seems only few people like you and me getting this. I tried chatgpt, it also pointed to this error. I first could not believe it, until I ran the exact code on the firebase official help doc, and it get the same error...... I just waste 2hour on bug from Google... maybe i should use dynamodb

nielsbox commented 9 months ago

Same also have this error and sometimes bumping the version of the library helps. But I'm already at latest now...

hansheng654 commented 9 months ago

Now i am facing firestore.Increment function does nothing to the actual data field, and no error at all. I expect better from Google.

akashrajkn commented 7 months ago

@hansheng654 Based on this comment, I changed urllib3 package version to 1.26.15. After this, I don't get the above issue. Maybe this helps you?

kolea2 commented 5 months ago

Hi @akashrajkn, thanks for filing this and providing an example. I tried this out with the latest version of the library (2.15.0) and was unable to reproduce this. Here's what I did:

document = db.collection("my-collection").document("number_doc")
f = float(1)
document.update({'num': firestore.Increment(f)})

I also tried different numbers, like 1.222 and was able to see the update successfully. Please let me know if you are still experiencing this issue after upgrading to the latest version. Thanks!

akashrajkn commented 5 months ago

@kolea2 Thanks for the reply! With version 2.15.0, I don't get the issue. Feel free to close this issue.

kolea2 commented 5 months ago

Awesome, thanks for confirming!

jstray commented 1 month ago

Getting this same message, version 2.17.0, trying to execute this line:

db.collection("users").document(user_id).set(user_record)

Writing to the emulator, not the actual cloud. Recently did a pip install -r requirements.txt and it seemed to break this, worked before. Unfortunately I don't have that log so can't say exactly what upgraded. Deprecating urllib3 to 1.26.15 was no help

message:

mailer_test.py:50: in create_test_data
    db.collection("users").document(user_id).set(user_record)
/usr/local/anaconda3/lib/python3.12/site-packages/google/cloud/firestore_v1/document.py:165: in set
    batch, kwargs = self._prep_set(document_data, merge, retry, timeout)
/usr/local/anaconda3/lib/python3.12/site-packages/google/cloud/firestore_v1/base_document.py:214: in _prep_set
    batch.set(self, document_data, merge=merge)
/usr/local/anaconda3/lib/python3.12/site-packages/google/cloud/firestore_v1/base_batch.py:112: in set
    write_pbs = _helpers.pbs_for_set_no_merge(

...
        if extractor.has_transforms:
            field_transform_pbs = extractor.get_field_transform_pbs(document_path)
>           set_pb.update_transforms.extend(field_transform_pbs)
E           TypeError: Not a cmessage

/usr/local/anaconda3/lib/python3.12/site-packages/google/cloud/firestore_v1/_helpers.py:723: TypeError