Opened 7 years ago

Closed 7 years ago

#27372 closed Bug (fixed)

inspectdb fails to inspect sqlite3 tables with foreign keys that have spaces

Reported by: samuller Owned by: Tim Graham <timograham@…>
Component: Core (Management commands) Version: 1.10
Severity: Normal 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

Running python manage.py inspectdb on an sqlite3 database with the following schema:

CREATE TABLE table_1 (
    id INTEGER PRIMARY KEY AUTOINCREMENT
);

CREATE TABLE table_2 (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    foreign_key_col INTEGER,
    FOREIGN KEY (foreign_key_col) REFERENCES table_1(id)
);

results in output missing tables with foreign keys:

class Table1(models.Model):
    id = models.IntegerField(primary_key=True, blank=True, null=True)  # AutoField?

    class Meta:
        managed = False
        db_table = 'table_1'
# Unable to inspect table 'table_2'
# The error was: 'NoneType' object has no attribute 'groups'

It turns out the regular expression (in django/db/backends/sqlite3/introspection.py) is not matching the string "FOREIGN KEY (foreign_key_col) REFERENCES table_1(id)" due to the single space character between KEY and the bracket. Depending on the schema input used to create the table there could be any amount of whitespace there.

Change History (6)

comment:1 by samuller, 7 years ago

Easy pickings: set

comment:2 by Tim Graham, 7 years ago

Component: Database layer (models, ORM)Core (Management commands)
Easy pickings: unset
Has patch: set
Needs tests: set
Summary: Inspectdb fails to inspect sqlite3 tables with foreign keysinspectdb fails to inspect sqlite3 tables with foreign keys that have spaces
Triage Stage: UnreviewedAccepted

PR (currently missing a test)

comment:3 by Saulius Žemaitaitis, 7 years ago

Owner: changed from nobody to Saulius Žemaitaitis
Status: newassigned

comment:4 by Saulius Žemaitaitis, 7 years ago

Needs tests: unset
Owner: Saulius Žemaitaitis removed
Status: assignednew

PR with tests based on the patch by samuller.

Last edited 7 years ago by Saulius Žemaitaitis (previous) (diff)

comment:5 by Michael Manfre, 7 years ago

Triage Stage: AcceptedReady for checkin

Patch now has a test and looks good.

comment:6 by Tim Graham <timograham@…>, 7 years ago

Owner: set to Tim Graham <timograham@…>
Resolution: fixed
Status: newclosed

In f28d29e8:

Fixed #27372 -- Fixed introspection of SQLite foreign keys with spaces in DDL.

Thanks samuller for the report and initial patch.

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