Hacker News new | ask | show | jobs
by nickjj 2039 days ago
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.

1 comments

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.