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

"id" field in document gets removed when creating a document #490

Closed
slenin opened this issue May 27, 2023 · 4 comments
Closed

"id" field in document gets removed when creating a document #490

slenin opened this issue May 27, 2023 · 4 comments

Comments

@slenin
Copy link

slenin commented May 27, 2023

Describe the bug
When a document has both "_id" and "id" field, the "id" field is removed. However, I am able to have a document with both fields when created through the Cloudant dashboard.

To Reproduce
sample_document = {
"_id": "70b63682-b93a-4c77-aad2-65501347265f4",
"id": "1",
"name": "Yamba",
"quantity": 10.5,
"sale": False,
}
document: Document = Document.from_dict(sample_document)
client.post_document(
db="testdb",
document=document
).get_result()

Document in the store is
{
"_id": "70b63682-b93a-4c77-aad2-65501347265f4",
"_rev": "1-d5a833fdf112ec7c7d85dde1cbed7878",
"name": "Yamba",
"quantity": 10.5,
"sale": false
}

Document.from_dict seems to remove the "id" field. Are there any workarounds to keep both fields?

Expected behavior
The document in the store should be
{
"_id": "70b63682-b93a-4c77-aad2-65501347265f4",
"_rev": "1-d5a833fdf112ec7c7d85dde1cbed7878",
"id": "1",
"name": "Yamba",
"quantity": 10.5,
"sale": false
}

Must gather (please complete the following information):

  • SDK Version 0.4.2
  • Python 3.11
  • IBM Cloudant
  • post_document
@slenin
Copy link
Author

slenin commented May 27, 2023

Using BinaryIO has expected behavior

sample_document = {
"_id": "70b63682-b93a-4c77-aad2-65501347265f4",
"id": "1",
"name": "Yamba",
"quantity": 10.5,
"sale": False,
}

docbytes = json.dumps(sample_document).encode('utf-8')
response = client.post_document(
db="testdb",
document=docbytes,
content_type='application/json'
).get_result()

@ricellis
Copy link
Member

The internal representation of the Document model does not allow for fields with the same names as the reserved (_ prefixed) document fields. Sorry that does not appear to be documented, I think it should be listed in our KNOWN_ISSUES and I'll look to get that fixed.

As you have already discovered one workaround is to use binary IO, which is the means by which the SDK allows for more flexible user-controlled serialization and deserialization. The other potential workarounds are to use a different name for the field (e.g. product_id or to embed the id in a nested object e.g. "data": {"id": 1, "name": …}.

As a note of caution the CouchDB and Cloudant APIs do sometimes return the _id in a field called id (e.g. in the response after writing a document or in view and query responses). So having fields called both _id and id may make an application hard to understand since ['id'] in one context could refer to something completely different than ['id'] in a different context.

@ricellis
Copy link
Member

@eiri eiri closed this as completed Oct 30, 2023
@ricellis
Copy link
Member

This problem has been resolved in version 0.8.0. From that version it is possible to have both _id and id properties in the same Document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants