Opened 7 years ago

Closed 7 years ago

#7750 closed (duplicate)

TextField in unique_together breaks with MySQL BLOB index without index key length

Reported by: benjaoming Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: unique_together
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I get this exception:

_mysql_exceptions.OperationalError: (1170, "BLOB/TEXT column 'text' used in key specification without a key length")

I'm not quite sure about what's the right thing to do. Apparently a text field can't be used in key combinations in MySQL and probably neither in other SQL flavors. So maybe Django should raise an exception and/or the documentation should include which kinds of fields that are valid for the unique_together meta attribute.

An example could be:

class Messages(models.model)
    subject = models.CharField(max_length=20)
    msg = models.TextField
    
    class Meta:
         unique_together = ("subject", "msg")

Change History (2)

comment:1 Changed 7 years ago by benjaoming

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Sorry. That wasn't a runnable example. This one is:

class Message(models.Model):
    subject = models.CharField(max_length=20)
    msg = models.TextField()
    
    class Meta:
         unique_together = ("subject", "msg")

comment:2 Changed 7 years ago by Simon Greenhill

  • Resolution set to duplicate
  • Status changed from new to closed
  • Summary changed from TextField in unique_together to TextField in unique_together breaks with MySQL BLOB index without index key length

Duplicate of the quite old bug #2495. The easy work-around is to leave unique=True off the model, and run ALTER TABLE manually to add the unique constraint.

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