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

.find() function does not work after a single use #34

Closed
avi-perl opened this issue Aug 15, 2021 · 7 comments
Closed

.find() function does not work after a single use #34

avi-perl opened this issue Aug 15, 2021 · 7 comments
Assignees
Labels
bug Something isn't working good first issue Good for newcomers hacktoberfest

Comments

@avi-perl
Copy link

Describe the bug
When writing data to the JSON db, after the first write to the file, the .find() method will return the record based on the ID. After adding a 2nd record and trying again, the IdNotFoundError error is thrown despite the record being in the DB!

How to reproduce
python 3.9.6

from pysondb import db

if __name__ == "__main__":
    db = db.getDb("test.json")
    while True:
        id = db.add({"dev": "test"})
        print(db.find(id))

Expected behavior
The record should be returning as expected.


This is a neat library, great work!

@avi-perl avi-perl changed the title .filter() function does not work after a single use .find() function does not work after a single use Aug 15, 2021
@avi-perl
Copy link
Author

Line 215 of db.py, the first instance of raise IdNotFoundError(pk) will raise after looking at the first item in the DB.

    def find(self, pk :int, objectify: bool = False) -> getType:
        with self.lock:
            try:
                with open(self.filename, "r", encoding="utf8") as db_file:
                    db_data = self._get_load_function()(db_file)
                for d in db_data["data"]:
                    if(d[self.id_fieldname])==self._cast_id(pk):
                        return (
                        d
                        if not objectify
                        else self._objectify(json.dumps({"data": d})).data
                        )
                    else:
                        raise IdNotFoundError(pk)

            except:
                raise IdNotFoundError(pk)

@fredysomy
Copy link
Member

Hi @avi-perl . Will get back to this issue , sort of busy now 😅

@fredysomy fredysomy added hacktoberfest bug Something isn't working good first issue Good for newcomers labels Sep 29, 2021
@LyndonFan
Copy link
Contributor

Hi, is the issue still open? I can try to work on it!

@fredysomy
Copy link
Member

Yes , this issue is still open and you can work on it, shall I assign it to you?

@LyndonFan
Copy link
Contributor

That would be great, thanks!

@fredysomy
Copy link
Member

Sure. Thanks , 🎉🎉

@fredysomy
Copy link
Member

@avi-perl , this issue is resolved in #47 .

Closing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers hacktoberfest
Projects
None yet
Development

No branches or pull requests

3 participants