Opened 9 years ago

Closed 4 years ago

#5014 closed Bug (fixed)

manage.py inspect db is not using the new DecimalField

Reported by: James <james_027@…> Owned by: nobody
Component: Core (Management commands) Version: master
Severity: Normal Keywords: inspectdb
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi,

I've use the manage.py inspectdb utility. It's very nice but it is still using the model.FloatField rather than model.DecimalField.

Thanks
james

Attachments (3)

inspect_decimal_field.diff (1.6 KB) - added by jmelesky 9 years ago.
5014-with-tests.diff (2.6 KB) - added by Claude Paroz 6 years ago.
Updated/completed patch with tests
5014-with-tests-2.diff (3.6 KB) - added by Claude Paroz 6 years ago.
Same patch with mysql introspection fix

Download all attachments as: .zip

Change History (14)

comment:1 Changed 9 years ago by Philippe Raoult

Triage Stage: UnreviewedAccepted

This model:

class DecimalTest(models.Model):
    test = models.DecimalField(max_digits=10, decimal_places=3)

will be introspected as:

class TimesheetDecimaltest(models.Model):
    id = models.IntegerField(primary_key=True)
    test = models.TextField() # This field type is a guess.
    class Meta:
        db_table = u'timesheet_decimaltest'

using activepython 2.5.1 under windows xp, with sqlite3 backend.

Could you please add more details about your config, especially the backend you used?

comment:2 Changed 9 years ago by jmelesky

Has patch: set

I've verified the problem on OSX, sqlite3, Py25. And i've verified that the problem is *not* present using MySQL 5ish, debian, Py24.

From the code, i'm guessing this is a problem with the sqlite introspection only. The Postgres and Oracle code both seem to already take DecimalFields into account (though i haven't tested this). I've attached a patch.

comment:3 Changed 9 years ago by Philippe Raoult

Can you reupload the patch ? it got lost by trac it seems (known issue over the weekend).

Changed 9 years ago by jmelesky

Attachment: inspect_decimal_field.diff added

comment:4 Changed 9 years ago by Ramiro Morales

Summary: manage.py inspect db is not using the new decimalFieldmanage.py inspect db is not using the new DecimalField

comment:5 Changed 6 years ago by Luke Plant

Needs tests: set

comment:6 Changed 6 years ago by Gabriel Hurley

Severity: Normal
Type: Bug

Changed 6 years ago by Claude Paroz

Attachment: 5014-with-tests.diff added

Updated/completed patch with tests

comment:7 Changed 6 years ago by Claude Paroz

Easy pickings: unset
Needs tests: unset
Patch needs improvement: set

I added a patch which adds guessed max_digits and decimal_places for SQLite (with a notice) because decimal fields in SQLite are simply mapped as real (float) type. This way, the output of inspectdb is valid.

The problem with the current patch is that it doesn't pass with MySQL. Strangely, my version of mysqldb (1.2.2-10+b1) is always returning the value + 2 in cursor.description[4] (precision). Is this a known mysqldb bug?

comment:8 Changed 6 years ago by Claude Paroz

Patch needs improvement: unset

After some more research, I think that this might be due to historically reasons. MySQL used to store decimal fields as strings, and the length returned was also counting a place for the dot and the sign, hence the + 2.
I'm attaching a patch that fixes this peculiarity in MySQL introspection file.

Changed 6 years ago by Claude Paroz

Attachment: 5014-with-tests-2.diff added

Same patch with mysql introspection fix

comment:9 Changed 5 years ago by Ramiro Morales

Component: Database layer (models, ORM)Core (Management commands)
Keywords: inspectdb added
UI/UX: unset

comment:10 Changed 4 years ago by Claude Paroz <claude@…>

In 51028f50b624efae273dcbe828ec80c1846c74dc:

Fixed getting max_digits for MySQL decimal fields

Refs #5014.

comment:11 Changed 4 years ago by Claude Paroz <claude@…>

Resolution: fixed
Status: newclosed

In dcf563071fad7fe286c6921c949639fc99f51351:

Fixed #5014 -- Guessed max_digits and decimal_places for SQLite

Decimal is treated as float on SQLite, hence inspectdb can only
guess max_digits and decimal_places arguments.

Note: See TracTickets for help on using tickets.
Back to Top