craftcms / craftcms/cms

Saving some textual fields (like Checkbox) can cause MySQL error

Open
#3,995 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug severity:minor
Dominant language
PHP
Stars
3.6k
Forks
705
Avg merge
1d 5h
Merged PRs (30d)
134

Description

Description

If MySQL is running in "strict mode" which is default in 5.7, then you can cause an error saving field settings in certain scenarios.

Steps to reproduce
  1. Install Craft on MySQL 5.7+
  2. Create a checkbox field (will apply to some other fields as well)
  3. Add enough checkbox options so that total character number is > 255 characters.
  4. Add the field to a section's field layout.
  5. Create an entry and select all of the checkbox options.
  6. Edit the checkbox field's settings and remove enough options so the total character count is < 255 characters.
  7. Try to save and you get the error:

SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'field_name' at row 879
The SQL being executed was: ALTER TABLE content CHANGE field_name field_name varchar(255)

If you disable MySQL strict mode it'll work, but because checkbox values are stored as JSON encoded data, it'll be invalid when MySQL truncates the data.

One possible solution would be to add an "Advanced" settings for affected fields where you can explicitly set the underlying database column type (like Redactor does). It would be set to "auto" by default to keep the current behavior but could be overridden to text, for example.

Additional info
  • Craft version: Craft 3
  • PHP version: 7.3.1
  • Database driver & version: MySQL 5.7+

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with MySQL 5.7+ strict mode by shrinking checkbox options after storing more than 255 characters. Trace the field storage and content-column alteration path; done means saving the reduced settings succeeds without truncating or invalidating the JSON data.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, php
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.