Hacker News new | ask | show | jobs
by solox3 2039 days ago
"CharField with huge max_length (5000)"

If I'm on postgres and actually want my comments to have a max length of 5000, is it suggesting that using CharField(max_length=5000) is a problem with my design? Using CharField / VARCHAR vs TextFIeld / TEXT should not have a performance difference in postgresql.

3 comments

I don't know about Django's ORM but normally I would use a TEXT field and then set a separate char_length CONSTRAINT on the field.

I think it's important to have the length constraint at the database level for data consistency's sake. Then you can choose to add in app level validations to make more friendly user error messages.

Changing a constraint's length is also a much easier database operation vs. changing a varchar's length.

A nice advantage of having a TextField with a check constraint, instead of a varchar, is that the locks required in Postgres to change the check constraint are less extreme than the ones required to change the length of varchar.

With a check constraint, we can use `NOT VALID` with a separate `VALIDATE` call, while a change to the varchar length requires an `ACCESS EXCLUSIVE` lock.

I think what they are trying to say, is that if you have a huge max_length it is possibly because you accept any type of text, and you felt the number was high enough.

If you use postgres, you want to restrict the length (even if it is 5000) ? => CharField, you don't care about the length => TextField

This covers it quite nicely https://django.doctor/advice/C2006
Thanks for replying, and yes that is the page about which I have questions.

* The error code C2006 assumes there is a clear divide between "smaller" and "larger" strings. What is smaller, and what is larger? Is 1000 large? Is 5000 large? Is 1000000 large? If I want my comment to be capped at 5000 characters to encourage healthy discussions but prevent spamming, is it large?

* If a field has no "known format" and that's where it draws the line between CharField and TextField, then why is the error C2006 still raised when TextField(max_length=5000) is used? Why does the validator recommend allowing anyone to insert entries of unlimited size?