Saving some textual fields (like Checkbox) can cause MySQL error
Nobody has claimed this yet.
- 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
- Install Craft on MySQL 5.7+
- Create a checkbox field (will apply to some other fields as well)
- Add enough checkbox options so that total character number is > 255 characters.
- Add the field to a section's field layout.
- Create an entry and select all of the checkbox options.
- Edit the checkbox field's settings and remove enough options so the total character count is < 255 characters.
- 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 TABLEcontentCHANGEfield_namefield_namevarchar(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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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