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']
5
  • duplicate of stackoverflow.com/questions/1827063/… Commented Dec 29, 2017 at 9:04
  • @SagunShrestha no it's not , as that person is using that field as a primary key but it's not the case here... Commented Dec 29, 2017 at 9: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)). Commented Dec 29, 2017 at 9: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) Commented Dec 29, 2017 at 10:14
  • @SagunShrestha that's mysql while we are trying to code a solution in Django. Commented Sep 30, 2019 at 0:25

1 Answer 1

1

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

models.TextField(max_length=1000)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.