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
250 stars 29 forks source link

Special field to query on "id" is not supported #160

Closed dhodun closed 1 year ago

dhodun commented 1 year ago

Firestore has the ability to query directly on a document ID using __name__, google.cloud.firestore_v1.field_path.FieldPath though this is not available in FireO.

https://stackoverflow.com/questions/48466028/query-firebase-firestore-documents-by-the-id/63803724#63803724

This is needed for queries where you want to filter on multiple document ids or you want to filter on a single document id and another field in a single query without having to pull down the document and inspect the contents.

This should be valid:

class Test(Model):
  name = TextField()

test1 = Test(name='test1')
test1.save()
Test.collection.filter('__name__', 'in',
                                    [test1.id]).get()

Or ideally

class Test(Model):
  name = TextField()

test1 = Test(name='test1')
test1.save()
Test.collection.filter('id', 'in',
                                    [test1.id]).get()

Actual behavior:

raise FieldNotFound(f'Field "{name}" not found in model "{cls.__name__}"')
fireo.fields.errors.FieldNotFound: Field "id" not found in model "Test"

It is worth noting that you have to use the in operator and cannot use ==

i.e. this seems not to be valid (even with firestore client). It will return 0 results in firestore emulator but error in Firestore.

class Test(Model):
  name = TextField()

test1 = Test(name='test1')
test1.save()
Test.collection.filter('id', '==', test1.id).get()

This should still throw the following error as you would get directly from the firestore client:

 raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.InvalidArgument: 400 __key__ filter value must be a Key
AxeemHaider commented 1 year ago

I check your PR and there was some problem it is not passing the tests you can check it here Test Report I made some changes and hopefully every thing is working now

AxeemHaider commented 1 year ago

Now you can pass id of document or a key.

Test.collection.filter("id", "in", [test.id]).fetch()

# OR with Key

Test.collection.filter("id", "in", [test.key]).fetch()

It will be great if you mention this feature in Documentation

AxeemHaider commented 1 year ago

update FireO version to v1.6.0