Closed (duplicate)
Project:
Drupal core
Version:
11.x-dev
Component:
base system
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
28 May 2020 at 12:08 UTC
Updated:
13 Mar 2025 at 12:25 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
alexpottThis patch allows you to use the DbDumpCommand and https://www.drupal.org/project/json_field without issue.
Comment #3
alexpottComment #4
alexpottComment #8
damienmckennaRerolled.
Comment #9
damienmckennaComment #10
damienmckennaIs it possible to alter the database type map through hooks, or is it too low-level? i.e. could json_field handle this change directly without needing to extend the MySQL driver?
Would there be any downside to committing this without fully extending the data type system to support JSON?
What are the next steps? Should we start with this being a recommended patch for sites that want to use JSON Field?
Comment #11
alexpott@DamienMcKenna well as part of the requirements for Drupal 10 is that all DBs support json in some way. So potentially this issue will have to be broadened to work out how to support all core supported db drivers. There is no alter to the field type map - it's very low level.
Comment #13
damienmckennaComment #14
damienmckennaMore simply - when a field is created using JSON Field and this patch, should the column in the database have the column "type" set to "json"? Because it's showing up as "longtext" in MariaDB 10.4 in ddev:
Comment #15
damienmckennaThe queries work as expected in MariaDB:
Comment #16
alexpott@DamienMcKenna the field type created by the json_field module depends on how you configure that module. If you select "JSON (stored as raw JSON in database)" then
the table looks like this:
Comment #17
alexpottAnd this table cannot be exported by core's database dump command because we're missing the mapping in Schema::getFieldTypeMap()
Comment #18
alexpottMariaDB does not store native json in the same way as MySQL - it stores it as longtext so it is unaffected by this issue... https://mariadb.com/kb/en/json-data-type/
Comment #19
damienmckennaThanks. I was doing some final testing of JSON Field prior to a 1.0 release and was confused by what I saw in ddev's default MariaDB database, but this explains it.
Comment #20
rgpublicWow, that's a bit unexpected and disappointing. If I understand correctly, this means that in MariaDB the whole new JSON support is in fact just a glorified string operation, right? I'm also surprised that they claim that their benchmarks don't show a performance difference. I would have thought that it's better to store sth. preparsed in binary form - especially when you try to lookup something.
Comment #21
alexpottFWIW we could add test coverage for this in \Drupal\Tests\system\Kernel\Scripts\DbDumpCommandTest - but actually since #3210913: DbDumpCommand fails when data type is not a mapped Drupal schema field name landed this is less of an issue as when we don't have a type in the map we'll just use what the database tells us.
That said, I still think we should do this issue.
Comment #22
kevinquillen commentedExciting to see this happening.
Does this mean that now we can do this instead of use a serialized blob store?
Other than this patch and query functions, is there anything else to be aware of?
Comment #23
smustgrave commentedMoving to NW for the test coverage. If the solution changes per the current discussion please update issue summary also.
Comment #24
rgpublicI wonder if it wouldn't be nice to have a "shortcut" so instead of this:
just this would be possible:
Comment #25
gapplePostgreSQL has two datatypes,
jsonandjsonbwith slightly different behaviour - it looks like MySQL'sjsontype is more similar to PostgreSQL'sjsonb, in that it does some normalization like removing duplicate object keys and extra whitespace when a value is stored in the field.Should the MySQL driver define both to match the current patch for the PostgresSQL driver?
The differing behaviour is probably something to note in documentation, and that
jsonbwill probably provide the best consistency of behaviour between db backends.Comment #26
anybodyComment #28
andypostLooks the most compatible way is to provide only
json:normalOOB and usemysql_typeandpgsql_typeto varyComment #29
andypostre-parenting
Comment #30
andypostHere's a patch with test for returned data types
The issue I found comparing to
sqliteandpgsqlis that syntax for->>operator is bit different- sqlite, pgsql -
select [field] -> "key1" ->> "key2"- mysql is
select [field] -> '$.key1.key1'or braces for array indexselect [field] -> '$.key1[0]'Comment #31
andypostFix cs
Comment #32
andypostsimplified tests
Comment #33
smustgrave commentedTests appear green
Verified the test locally running on ddev with mysql:8.0 I get
Undefined array key "json:normal"
Comment #35
smustgrave commentedRandom unrelated failure that was popping up today.
Comment #36
bradjones1Per #3343634-14: Add "json" as core data type to Schema and Database API this is being consolidated into the parent issue for a single change.