Auto-truncating fields at max_length in Django CharFields

前端 未结 4 968
夕颜
夕颜 2020-12-15 16:40

I have a field that has a max_length set. When I save a model instance, and the field\'s value is greater than max_length, Django enforces that

4条回答
  •  一个人的身影
    2020-12-15 16:58

    That seems inelegant and there must be a better way.

    The only reason the truncate behavior ever happens in the first place is because of MySQL's failure to adhere to the SQL Standard. The throwing of an exception is the correct response when attempting to INSERT a string into a VARCHAR field that is not wide enough to hold it. MySQL truncates and inserts instead.

    If you want to silently corrupt your data, then you'll have to do it manually somehow--PostgreSQL will never do it for you.

提交回复
热议问题