I keep getting this error: (1366, "Incorrect string value: '\\xF0\\x9F\\x8E\\xA5 W...' for column 'content' at row 1")
I've created a model called Post in my Django application which makes use of the Martor markdown editor to collect the post content as markdown syntax and translate/render them on my post template. I keep getting the 1366 error, I've already browsed through several solutions here on stackoverflow but nothing seems to be working... The most common solutions I have seen is altering the content table to change the character set to utf8mb4.
When I ran the code:
ALTER TABLE myapp_post CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
I get the error:
ERROR 1071 (42000): Specified key was too long; max key length is 3072 bytes
The only way around that was to enter the query:
ALTER TABLE myapp_post CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin;
Which work but I still get the same error.
Here are my codes for the application
# models.py
class Post(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=180)
title_slug = models.SlugField(max_length=900)
date = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
content = MartorField()
post_image = models.ImageField(upload_to='images/post_images/')
objects = models.Manager()
def get_absolute_url(self):
return reverse('post', kwargs={'title_slug': self.title_slug})
def __str__(self):
return ' by '.join(map(str, [self.title, self.author.user.username]))
# forms.py
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = (
'title', 'content', 'post_image'
)
# views.py
def create_post(request):
if request.method == 'POST':
form = PostForm(request.POST, request.FILES)
if form.is_valid():
cd = form.cleaned_data
title, content, post_image= (
cd['title'], cd['content'], cd['post_image']
)
Post.objects.create(
title=title, title_slug='-'.join(title.lower().split(' ')),
date=datetime.now(), author=Author.objects.get(user=request.user),
content=content, post_image=post_image
).save()
messages.success(request, f'Post "{title}" created successfully!')
return HttpResponseRedirect(reverse('index'))
else:
messages.error(request, 'This form isn\'t valid!')
return HttpResponseRedirect(reverse('create_post'))
else:
form = PostForm()
return ___
-- DDL for myapp_database.myapp_post
CREATE TABLE `myapp_post` (
`id` int NOT NULL AUTO_INCREMENT,
`title` varchar(180) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`title_slug` varchar(900) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`date` datetime(6) NOT NULL,
`content` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`post_image` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`author_id` int NOT NULL,
CONSTRAINT `myapp_post_author_id_84b_fk_myapp_author_id` FOREIGN KEY (`author_id`) REFERENCES `myapp_author` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
The form works without an emoji present in the content field which I get is the reason for the change in SQL table character set but I intend on having the contents containing emojis. So I will also appreciate work arounds.
I hope to get a definite solution from anyone. Thanks in anticipation 🙂.