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

feat: ✨ add strict parameter to FastCRUD get method #54

Merged
merged 6 commits into from
Apr 19, 2024

Conversation

dubusster
Copy link
Contributor

@dubusster dubusster commented Apr 16, 2024

Description

This PR aims to add a parameter strict to FastCRUD get method.
Indeed, when one want to retrieve some instance with some filters, and there are many results, the get method returns the first it encounters but doesn't warn the user of the other ones in the database. It can be misleading when you expect only one row in your database but there are a lot of duplicates in the table and you get no error.

So, if once needs it, it can use the strict parameter in get method to get one or none element.

class Model(Base):
    id: int = Column(Integer)
    name: int = Column(String)

model1 = Model(id=1, name="Igor")
model2 = Model(id=2, name="Igor")

crud = FastCRUD(Model)

db.add(model1)
db.add(model2)
await db.commit()

await crud.get(db, id=1) # --> gives model1
await crud.get(db, id=3) # --> gives None
await crud.get(db, name="Igor") # --> gives model1 because first in db
await crud.get(db, name="Igor", strict=True) # --> raises a MultipleResultError because there are many results with the same name

strict is by default on False to not disturb current behaviour 😄

Changes

fast_crud.py: I added the strict parameter in the get method

Tests

I added test_get_strict_existing_record in test_get.py for sqlalchemy and SQLModel

Checklist

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • I have added necessary documentation (if appropriate).
  • I have added tests that cover my changes (if applicable).
  • All new and existing tests passed.

Additional Notes

@dubusster dubusster marked this pull request as ready for review April 16, 2024 06:44
Copy link

codecov bot commented Apr 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (31a2066) to head (86ecb4a).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #54   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           64        64           
  Lines         4166      4181   +15     
=========================================
+ Hits          4166      4181   +15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@igorbenav igorbenav self-assigned this Apr 18, 2024
@igorbenav igorbenav added enhancement New feature or request FastCRUD Methods Related to FastCRUD methods labels Apr 18, 2024
@igorbenav
Copy link
Owner

Nice one, @dubusster! I'm wondering if using the strict is the best option, maybe one_or_none or even return_one_or_none, since it's more explicit and clearly shows what it's doing once it's set to True.

@dubusster
Copy link
Contributor Author

Thanks @igorbenav ! Indeed, one_or_none seems more straight forward and similar to what is in sqlalchemy. It will be less confusing. 😄

@igorbenav igorbenav merged commit 427806e into igorbenav:main Apr 19, 2024
9 checks passed
@dubusster dubusster deleted the strict-get branch April 19, 2024 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request FastCRUD Methods Related to FastCRUD methods
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants