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

fix(#634): added a generate function to support SQL Numeric field #636

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

nisemenov
Copy link
Contributor

Description

  • a function for generate random value for SQL NUMERIC type has been added;
  • appropriate tests have been added.

I have decided to add a new separate function to work with only SQL NUMERIC type like this, because Pydantic Decimal Constraints works in a slightly different way.

Also this function does not allow to use a negative scale and a scale larger than the declared precision. This can be used only with the SQL standard requirements (the scale must be in the range 0 to precision). And generated values can be either positive or negative.

Closes

@nisemenov nisemenov requested a review from guacs as a code owner January 27, 2025 13:15
@nisemenov nisemenov changed the title fix: added a generate function to support SQL Numeric field (#634) fix(#634): added a generate function to support SQL Numeric field Jan 27, 2025
@adhtruong
Copy link
Collaborator

adhtruong commented Jan 28, 2025

Based on examples and docs linked, from Pydantic

class Foo(BaseModel):
precise: Decimal = Field(max_digits=5, decimal_places=2)

foo = Foo(precise=Decimal('123.45')

and Postgres docs

So the number 23.5141 has a precision of 6 and a scale of 4.

how do these differ? Is this for trailing zero handling?

Based on failing example from issue it looks like an issue in generating the values themselves. Would this fix or similar apply to decimal handling too?

@nisemenov
Copy link
Contributor Author

nisemenov commented Jan 31, 2025

...

how do these differ? Is this for trailing zero handling?

Based on failing example from issue it looks like an issue in generating the values themselves. Would this fix or similar apply to decimal handling too?

Hi @adhtruong and thanks for reviewing!
Yes, it was my mistake, sorry for that. I didn't fully figure this topic out.

The main thing I have done in last commits:

  • added a default value for scale=0 in SQLA module, this field is different from Pydantic (because SQL docs say "NUMERIC(precision) selects a scale of 0.");
  • added a constraint check via max_digits and decimal_places to get_value_or_none func.;
  • changed handle_decimal_length func.;
  • decimal_places can be equal to max_digits.

Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/polyfactory-docs-preview/636

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

Successfully merging this pull request may close these issues.

Bug: handle_constrained_decimal not enforcing SQL numeric type precision properly
2 participants