Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#9736 closed (fixed)

Primary key with space in name generates invalid constraint SQL

Reported by: russellm Owned by: russellm
Component: Core (Management commands) Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The following models:

class Clue(models.Model):
   clue_id = models.AutoField(primary_key=True)
   entryRef = models.ForeignKey('Entry', db_column = 'Entry Ref')
   clue = models.CharField(max_length=150)

class Entry(models.Model):
   entry_id = models.AutoField(primary_key=True, db_column='Entry ID')
   entry = models.CharField(unique=True, max_length=50)

can't be created using syncdb. This is due to the SQL generated for the deferred foreign key constraint. The following is the SQL generated for Postgres:

ALTER TABLE "app2_clue" ADD CONSTRAINT Entry Ref_refs_Entry ID_692e28e3 FOREIGN KEY ("Entry Ref") REFERENCES "app2_entry" ("Entry ID") DEFERRABLE INITIALLY DEFERRED;

Analogous code is generated for MySQL The problem is the name of the reference: Entry Ref_refs_Entry ID_692e28e3. While all other uses of column names are quoted, the constraint name (which is derived from the column names) is not quoted. As a result, the constraint name is invalid.

Change History (3)

comment:1 Changed 6 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to russellm
  • Patch needs improvement unset
  • Status changed from new to assigned
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 6 years ago by russellm

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

(In [9543]) Fixed #9736 -- Added quoting to the SQL constraint names generated during table creation. This is to accommodate primary keys with spaces.

comment:3 Changed 6 years ago by russellm

(In [9545]) [1.0.X] Fixed #9736 -- Added quoting to the SQL constraint names generated during table creation. This is to accommodate primary keys with spaces.

Merge of [9543] from trunk.

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