Code

Opened 6 years ago

Closed 6 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")

Attachments (0)

Change History (2)

comment:1 Changed 6 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 6 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.