Sample Header Ad - 728x90

Sensible max_length for Postgres db charfields

2 votes
1 answer
2333 views
I am using Django with a Postgres backend. In the Django models, I have to set the max_length attribute on the Charfields, then the Django ORM deals with the database in the backend. In a lot of cases, those are rather small fields (e.g. say names, or city names etc.) that I don't expect would be very long. That application deals with existing data that isn't particularly cleaned. So I don't want to set something like name = models.Charfield(max_length=50, ...) Just to realize that someone triple wrote his name in that field and that the field is 75 characters long. On the other hand, I don't want to set huge values and needlessly increase database size just to save me a little bit of trouble. My current thinking is that Postgres probably reserves some minimal amount of space (e.g. likely that max_length=10 and max_length=15 both take the same storage), so I would set the max_length to that minimal size (or low multiple thereof). What do you recommend and why?
Asked by logicOnAbstractions (143 rep)
Nov 1, 2021, 05:50 PM
Last activity: Nov 2, 2021, 06:37 AM