Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#30115 closed Bug (fixed)

inspectdb on SQLite crashes when introspecting varchar primary key with TypeError: unhashable type: 'dict'

Reported by: Can Sarıgöl Owned by: Nick Pope
Component: Database layer (models, ORM) Version: 2.2
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I created a project with version 2.2a1 and run migrate after then run inspectdb. I got this error

class DjangoSession(models.Model):
Traceback (most recent call last):
  File "./manage.py", line 21, in <module>
    main()
  File "./manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/can/Documents/Contribute_projects/Tests/django2_2_alfa2/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/can/Documents/Contribute_projects/Tests/django2_2_alfa2/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/can/Documents/Contribute_projects/Tests/django2_2_alfa2/venv/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/can/Documents/Contribute_projects/Tests/django2_2_alfa2/venv/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/Users/can/Documents/Contribute_projects/Tests/django2_2_alfa2/venv/lib/python3.7/site-packages/django/core/management/commands/inspectdb.py", line 34, in handle
    for line in self.handle_inspection(options):
  File "/Users/can/Documents/Contribute_projects/Tests/django2_2_alfa2/venv/lib/python3.7/site-packages/django/core/management/commands/inspectdb.py", line 131, in handle_inspection
    field_type, field_params, field_notes = self.get_field_type(connection, table_name, row)
  File "/Users/can/Documents/Contribute_projects/Tests/django2_2_alfa2/venv/lib/python3.7/site-packages/django/core/management/commands/inspectdb.py", line 239, in get_field_type
    field_type = connection.introspection.get_field_type(row.type_code, row)
  File "/Users/can/Documents/Contribute_projects/Tests/django2_2_alfa2/venv/lib/python3.7/site-packages/django/db/backends/sqlite3/introspection.py", line 65, in get_field_type
    if description.pk and field_type in {'BigIntegerField', 'IntegerField'}:
TypeError: unhashable type: 'dict'

Change History (11)

comment:1 by Can Sarıgöl, 5 years ago

Summary: inspectdb, TypeError: unhashable type: 'dict'inspectdb for sqlite3, TypeError: unhashable type: 'dict'

comment:2 by Carlton Gibson, 5 years ago

Component: UncategorizedDatabase layer (models, ORM)
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

OK, yes, thanks for the report.

This works against 2.1.5 but errors as reported against 2.2a1:

django-admin startproject ticket_30115
cd ticket_30115/
./manage.py migrate
./manage.py inspectdb


comment:4 by Can Sarıgöl, 5 years ago

Last edited 5 years ago by Tim Graham (previous) (diff)

comment:5 by Tim Graham, 5 years ago

Easy pickings: unset
Needs tests: set
Summary: inspectdb for sqlite3, TypeError: unhashable type: 'dict'inspectdb on SQLite crashes with TypeError: unhashable type: 'dict'

Is that code untested or this caused by a version of SQLite that's untested on Jenkins?

comment:6 by Can Sarıgöl, 5 years ago

Owner: changed from nobody to Can Sarıgöl
Status: newassigned

I am adding test for it

comment:7 by Carlton Gibson, 5 years ago

>>> import sqlite3
>>> sqlite3.sqlite_version
'3.24.0'

Bisected to a35d2a4510d5beec398b1007aaa26492d6aedf97 which was Refs #23748

Last edited 5 years ago by Carlton Gibson (previous) (diff)

comment:8 by Nick Pope, 5 years ago

Sorry about this. I clearly forgot to take the special case of a tuple of (field_type, field_params_dict) into account, field_params_dict being the source of the error.

It turns out that this special case was a hack to handle max_length for CharField on SQLite only which was introduced by 9ede371c85a97406eecb19974ab01163839636a0.

Since a9a773ff38a9fd28085e8b62b8a160c4e2ea5efb, size is returned by get_table_description() on SQLite so row.internal_size is always overriding the max_length returned in field_params_dict.

Here is a PR that fixes the issue by removing this legacy approach to handling max_length.

comment:9 by Nick Pope, 5 years ago

Needs tests: unset
Owner: changed from Can Sarıgöl to Nick Pope

I think no tests should be needed as we're removing the code that caused the broken behaviour.

comment:10 by Tim Graham, 5 years ago

Summary: inspectdb on SQLite crashes with TypeError: unhashable type: 'dict'inspectdb on SQLite crashes when introspecting varchar primary key with TypeError: unhashable type: 'dict'
Triage Stage: AcceptedReady for checkin

comment:11 by Tim Graham <timograham@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In bff748df:

Fixed #30115 -- Fixed SQLite introspection crash with a varchar primary key.

Removed obsolete max_length handling for CharField that caused the issue.
Regression in a35d2a4510d5beec398b1007aaa26492d6aedf97.

comment:12 by Tim Graham <timograham@…>, 5 years ago

In e53e642:

[2.2.x] Fixed #30115 -- Fixed SQLite introspection crash with a varchar primary key.

Removed obsolete max_length handling for CharField that caused the issue.
Regression in a35d2a4510d5beec398b1007aaa26492d6aedf97.

Backport of bff748df3e1e1c0077e02df2b77bda2b827ad129 from master.

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