Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
NativeJSONItem
field sets 'type' => 'json'
in its schema definition. In case you have json_native
field and running tests with SQLite database this results in an exception:
Undefined array key "json:normal"
/web/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php:142
/web/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php:97
/web/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php:61
/web/core/lib/Drupal/Core/Database/Schema.php:616
/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php:421
The reason is that json
datatype does not exist in SQLite https://www.sqlite.org/datatype3.html.
Steps to reproduce
* Add json_native
field to an entity
* Run tests with SQLite database
* Undefined array key "json:normal"
message occurs
Proposed resolution
Add 'sqlite_type' => 'text'
in NativeJSONItem
class.
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#13 | json_field-n3252426-13.patch | 2.55 KB | DamienMcKenna |
#8 | 3252426-sqlite-json.patch | 4.12 KB | geek-merlin |
Issue fork json_field-3252426
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
mfbIronically NativeBinaryJSONItem has
'type' => 'text'
so I hadn't noticed this. I'm not sure if the proper fix is setting sqlite_type or type here :) Setting type would make it work like NativeBinaryJSONItem.Comment #3
geek-merlinCross-checking this with a current installation.
I can create a field and it seems to work well.
The DB schema looks strange though, the value column does not show a key.
So yes, looks like still an issue.
Comment #4
mfbHere's a test-only patch based on the steps to reproduce; should pass on mysql and fail on sqlite.
Comment #5
mfbOk the test seems to be working. I'm not sure what the preferred fix is so setting to "needs review".
If
'type' => 'json'
is supported by any contrib db backends out there, then that should be used here and'sqlite_type' => 'text'
should be added. Or it could be changed to'type' => 'text'
which is how NativeBinaryJSONItem works.Comment #7
geek-merlin@mfb: Wow, you're a real test hero!
Then here's the fix too, in a MR.
Comment #8
geek-merlinWTF? No tests on MR? OK, here a patch.
Comment #9
geek-merlinComment #10
geek-merlinDaring to RTBC (although technically i copied a line to the solution).
Test is proven to be red in #4, now green.
Comment #11
mfbThere is no issue testing enabled for this project, maybe a maintainer could configure that
Comment #12
DamienMcKennaI read through https://www.sqlite.org/json1.html and was surprised to see the following:
So yes, despite the fact that SQLite supports JSON, unlike MySQL and PostgreSQL it doesn't support a JSON data type.
This looks good, let's add it.
Comment #13
DamienMcKennaRerolled after #3281633 was committed.
Comment #14
DamienMcKennaCommitted. Thank you.
Comment #17
geek-merlin