Opened 6 years ago

Closed 6 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 Changed 6 years ago by samuller

Easy pickings: set

comment:2 Changed 6 years ago by Tim Graham

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 Changed 6 years ago by Saulius Žemaitaitis

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

comment:4 Changed 6 years ago by Saulius Žemaitaitis

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

PR with tests based on the patch by samuller.

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

comment:5 Changed 6 years ago by Michael Manfre

Triage Stage: AcceptedReady for checkin

Patch now has a test and looks good.

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

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