I have some decimal fields in my model that looks like this
amount = models.DecimalField(max_digits=999999999, decimal_places=2, default=0.00)
After seeing some posts i found out that reducing the max_digits=999999999
to some max_digits=1000
would fix the error (I haven’t tried it though.), the issue now is that amount
can be greater than 1000, NOTE: amount is currency. So a user can have amount greater than 1000 since the max_digits=1000
there would probably bee a problem in the future, how can i fix this?
Max digits refers to the number of digits, not the value of the number. The number 123456 has 6 digits, and so would need max_digits=6
(or more).
The number you’re trying to use above, 999999999
, only needs max_digits=9
.
Additional edit: The number 8888888888.77
would require max_digits=12
, as max_digits
needs to include the number of decimal places to the right of the decimal point.
2 Likes
Okay thanks, i have corrected that, now i am trying to migrate
, but it still shows this error, do i have to delete my migrations folder or something?
Update 1
I deleted the migrations folder for all the apps and ran python manage.py migrate
, i got this ne error django.db.migrations.exceptions.CircularDependencyError: userauths.0001_initial, core.0001_initial
Did you run a makemigrations after making the change but before running migrate?
yes i did run a makemigrations
Arbitrarily deleting migrations is an extremely bad idea.
Drop and recreate your database, delete all migrations from your project, run an initial migrate to rebuild the system tables, then rerun makemigrations and migrate.
Also See:
is it recommended to directly change the migrations file?
I still got this error django.db.utils.DataError: NUMERIC precision 999999999 must be between 1
after following the steps above.
But when i change the max_digits=99999999
to max_digits=12
in the migrations file, it starts working. would still have a problem in the future (i’m in production level for the application)
Before
migrations.AddField(
model_name='company',
name='wallet',
field=models.DecimalField(decimal_places=2, default=0.0, max_digits=999999999),
),
After
migrations.AddField(
model_name='company',
name='wallet',
field=models.DecimalField(decimal_places=2, default=0.0, max_digits=12),
),
It’s creating the postgres tables successfully
Before they are used and applied to the database, yes. There are times when you may even need to manually create migrations.
I don’t see how you could have possibly gotten this error if you fixed your code then deleted and recreated the migrations.