Opened 17 years ago

Closed 12 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: dev
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 17 years ago.
5014-with-tests.diff (2.6 KB ) - added by Claude Paroz 14 years ago.
Updated/completed patch with tests
5014-with-tests-2.diff (3.6 KB ) - added by Claude Paroz 14 years ago.
Same patch with mysql introspection fix

Download all attachments as: .zip

Change History (14)

comment:1 by Philippe Raoult, 17 years ago

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 by jmelesky, 17 years ago

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 by Philippe Raoult, 17 years ago

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

by jmelesky, 17 years ago

Attachment: inspect_decimal_field.diff added

comment:4 by Ramiro Morales, 17 years ago

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

comment:5 by Luke Plant, 14 years ago

Needs tests: set

comment:6 by Gabriel Hurley, 14 years ago

Severity: Normal
Type: Bug

by Claude Paroz, 14 years ago

Attachment: 5014-with-tests.diff added

Updated/completed patch with tests

comment:7 by Claude Paroz, 14 years ago

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 by Claude Paroz, 14 years ago

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.

by Claude Paroz, 14 years ago

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

Same patch with mysql introspection fix

comment:9 by Ramiro Morales, 13 years ago

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

comment:10 by Claude Paroz <claude@…>, 12 years ago

In 51028f50b624efae273dcbe828ec80c1846c74dc:

Fixed getting max_digits for MySQL decimal fields

Refs #5014.

comment:11 by Claude Paroz <claude@…>, 12 years ago

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