0

I am trying to create a model using django 1.11 and mysql(latest) as my backend using mysqlclient. I have searched various blogs and docs but was still not able to find my solution. This is my code Posts.models.py Please forgive the indentation error here if any.

class Post(models.Model):
     user=models.ForeignKey(User,related_name='posts',
     on_delete=models.CASCADE)
     created_at=models.DateTimeField(auto_now=True)
     message=models.TextField()
     group=models.ForeignKey(Group,related_name='posts',
     null=True,blank=True,on_delete=models.CASCADE)

     def __str__(self):
        return self.message

     def save(self,*args,**kwargs):
        super().save(*args,**kwargs)

     def get_absolute_url(self):
        return reverse('posts:single',kwargs{'username':self.user.username,'pk':self.pk})

     class Meta:
         ordering=['-created_at']
         unique_together=['user','message']
MinnuKaAnae
  • 1,646
  • 3
  • 23
  • 35
  • duplicate of https://stackoverflow.com/questions/1827063/mysql-error-key-specification-without-a-key-length – Sagun Shrestha Dec 29 '17 at 09:04
  • @SagunShrestha no it's not , as that person is using that field as a primary key but it's not the case here... – Vishwesh Singh Dec 29 '17 at 09:06
  • 1
    It's very similar to the duplicate question. You have `message` in `unique_together`, but MySQL can't create the index because it's a `TextField`. You can either remove it from `unique_together`, or change it (e.g. to `models.CharField(max_length=200)`). – Alasdair Dec 29 '17 at 09:40
  • @Alasdair Ok thanks got it, but just in case what to do when i have to index full text field.(CharField is not option as it is very small field from my project perspective) – Vishwesh Singh Dec 29 '17 at 10:14
  • @SagunShrestha that's mysql while we are trying to code a solution in Django. – User Sep 30 '19 at 00:25

1 Answers1

1

Set length to the text field. It did work for me. Like

models.TextField(max_length=1000)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103