Opened 8 years ago

Closed 2 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 7 years ago.
5014-with-tests.diff (2.6 KB) - added by claudep 4 years ago.
Updated/completed patch with tests
5014-with-tests-2.diff (3.6 KB) - added by claudep 4 years ago.
Same patch with mysql introspection fix

Download all attachments as: .zip

Change History (14)

comment:1 Changed 7 years ago by PhiR

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 7 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 7 years ago by PhiR

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

Changed 7 years ago by jmelesky

comment:4 Changed 7 years ago by ramiro

  • Summary changed from manage.py inspect db is not using the new decimalField to manage.py inspect db is not using the new DecimalField

comment:5 Changed 4 years ago by lukeplant

  • Needs tests set

comment:6 Changed 4 years ago by gabrielhurley

  • Severity set to Normal
  • Type set to Bug

Changed 4 years ago by claudep

Updated/completed patch with tests

comment:7 Changed 4 years ago by claudep

  • 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 4 years ago by claudep

  • 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 4 years ago by claudep

Same patch with mysql introspection fix

comment:9 Changed 4 years ago by ramiro

  • Component changed from Database layer (models, ORM) to Core (Management commands)
  • Keywords inspectdb added
  • UI/UX unset

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

In 51028f50b624efae273dcbe828ec80c1846c74dc:

Fixed getting max_digits for MySQL decimal fields

Refs #5014.

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

  • Resolution set to fixed
  • Status changed from new to closed

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