You have just found a new error in your monitoring (you do use error tracking, right?):
django.db.utils.DataError: value too long for type character varying(255)
After banging on the keyboard for a couple of minutes, you found out that it happens when your editors want to embed an Instagram post.
Now, what do you do? The Embed
model is very internal to Wagtail and not really customizable.
Fret not, for there is a way. It's not a pretty way, but it is one. At least until this matter will be resolved.
Preparation is everything
First, a word on why the limit is 255 characters to begin with: it allows for the broadest database compatibility. All databases do support up to 255 characters when it comes to their VARCHAR
datatype. Some support even more!
This also brings me to the most important point: the approach below is only tested with Postgresql. We don't use anything else at Codista. It might work with your database though. Please check if it supports VARCHAR
with more than 255 characters.
Walking The Path Of The Monkey
Yeah, you guessed right. We will do some ugly monkey patching. Again, this is not pretty, but it gets the job done. Also, this is only temporary (said every developer ever).
Now, lets add the following lines to one of your models.py
. You can actually put this wherever. It just has to be read by Python on start-up. So an __init__.py
will also do.
# some models.py
from wagtail.embeds.models import Embed
# Instagram returns URLs longer than 255 chars. Not yet supported by Wagtail
# but it will be!
Embed._meta.get_field("thumbnail_url").max_length = 500
That's mostly it. Now run ./manage.py makemigrations
. You will notice that Django now creates a migration inside the wagtailembeds
app.
❯ ./manage.py makemigrations
Migrations for 'wagtailembeds':
/myvenv/lib/python3.8/site-packages/wagtail/embeds/migrations/0006_auto_20201013_1623.py
- Alter field thumbnail_url on embed
If you now run ./manage.py migrate
, the problem will be fixed. Job done!
A Word on Updates
Please beware that this approach can, and will, lead to something like shown below when you update Wagtail.
❯ ./manage.py migrate
CommandError: Conflicting migrations detected; multiple leaf nodes in the migration graph: (0006_auto_20201013_1623, 0006_wagtail_fixed_it in wagtailembeds).
To fix them run 'python manage.py makemigrations --merge'
What happened? Answer: You are updating Wagtail and the Wagtail Embeds app now ships a migration number 0006. It collides with the one we created.
Before I tell you how to fix this, take a moment and check if that new migration contains a fix for the thumbnail url. The wagtail team is constantly improving and fixing bugs.
If it doesn't, here's how to make everything work again – without having to merge migrations as told by the migrate command
- Delete your custom migration (e.g.
/myvenv/lib/python3.8/site-packages/wagtail/embeds/migrations/0006_auto_20201013_1623.py
, your number and timestamp will look different) - Run
./manage.py migrate
. This should now work as we got rid of the conflict we brought upon ourselves. - When that's done, you run
./manage.py makemigrations
again. This will, again, create the migration for thethumbnail_url
. - Run
./manage.py migrate
again to fix yourthumbnail_url max_length
issue.
Simply adhere to this process for every time you encounter this specific issue and you should be good.
If this doesn't work for you, or you have found another approach to fix this, feel free to hit us up on Twitter.